Create the Other Pages

Now that I have my basic page structure down, it’s time to start looking at the other
pages in my site. Working with the styles I’ve already created (and creating new ones
as I need to), I’ll create the following pages in my layout:
• Blog listing, with the sidebars as used in my two-sidebar layout
• Category page, based on the blog listing page
• Project page, with associated images and text
• Project listing, with images and a brief project description
• Contact page
• A 404 (page not found) and 403 (access denied) page
• The home page, with associated blocks and callout areas
This should cover most of the pages that I will be setting up in the Drupal implementation,
and give me more than enough to work with. Many of the pages will feed each
other—for example, my blog listing page will start with my two-sidebar layout, and
change the listing, and then the category page will follow from that. However, the
project pages, being highly image/case study focused, will require special treatment,
including putting some thought into how I’m going to organize the projects,

Single Node Pages with Sidebars

The point of starting off your template with a node page that doesn’t have sidebars is
this: you will inevitably have a page like this somewhere on your site. And many designers,
well-meaning as they are, end up forgetting this and assume there will be 1–2 sidebars
on the page. As Drupal’s default behavior reflows the text to fill the entire page when
there are no sidebars, this results in these pages having long and drastically uncomfortable
line lengths.
That said, it’s safe to assume that most pages will have at least one sidebar, and that
the sidebars will contain different types of blocks, for example:
• A list of node titles or categories
• Static text or images
• A tag cloud or something similar
• Callout boxes, like a contact form or customer testimonial
Therefore, while I’m working on my node pages, I should also take a look at how these
different types of sidebar blocks will be styled, and how I’ll set up both one- and twosidebar
layouts.

Storing Complex Data Types

You can use sessions to store complex data types such as objects and arrays simply by treating them as standard variables, as this code shows:
    $myarr["0"] = "Sunday";
    $myarr["1"] = "Monday";
    $myarr["2"] = "Tuesday";
    $myarr["3"] = "Wednesday";
    $myarr["4"] = "Thursday";
    $myarr["5"] = "Friday";
    $myarr["6"] = "Saturday";

    $_SESSION["myarr"] = $myarr;

Cookies Versus Sessions?

 Cookies

The setcookie( ) call needs to be before the HTML form because of the way the web works. HTTP operates by sending all "header" information before it sends "body" information. In the header, it sends things like server type (e.g., "Apache"), page size (e.g., "29019 bytes"), and other important data. In the body, it sends the actual HTML you see on the screen. HTTP works in such a way that header data cannot come after body datayou must send all your header data before you send any body data at all.
Cookies come into the category of header data. When you place a cookie using setcookie( ), your web server adds a line in your header data for that cookie. If you try and send a cookie after you have started sending HTML, PHP will flag serious errors and the cookie will not get placed.
There are two ways to correct this:
  • Put your cookies at the top of your page. By sending them before you send anybody data, you avoid the problem entirely.
  • Enable output buffering in PHP. This allows you to send header information such as cookies wherever you likeeven after (or in the middle of) body data. Output buffering is covered in depth in the following chapter.
The setcookie( ) function itself takes three main parameters: the name of the cookie, the value of the cookie, and the date the cookie should expire. For example:
 
setcookie("Name", $_POST['Name'], time( ) + 31536000);
 
In the example code, setcookie( ) sets a cookie called Name to the value set in a form element called Name. It uses time( ) + 31536000 as its third parameter, which is equal to the current time in seconds plus the number of seconds in a year, so that the cookie is set to expire one year from the time it was set.
Once set, the Name cookie will be sent with every subsequent page request, and PHP will make it available in $_COOKIE. Users can clear their cookies manually, either by using a special option in their web browser or just by deleting files.

print $_COOKIE["Name"];


 Sessions

Sessions store temporary data about your visitors and are particularly good when you don't want that data to be accessible from outside of your server. They are an alternative to cookies if the client has disabled cookie access on her machine, because PHP can automatically rewrite URLs to pass a session ID around for you.

 Starting a Session

A session is a combination of a server-side file containing all the data you wish to store, and a client-side cookie containing a reference to the server data. The file and the client-side cookie are created using the function session_start( ) it has no parameters but informs the server that sessions are going to be used.
When you call session_start( ), PHP will check to see whether the visitor sent a session cookie. If it did, PHP will load the session data. Otherwise, PHP will create a new session file on the server, and send an ID back to the visitor to associate the visitor with the new file. Because each visitor has his own data locked away in his unique session file, you need to call session_start( ) before you try to read session variablesfailing to do so will mean that you simply will not have access to his data. Furthermore, as session_start( ) needs to send the reference cookie to the user's computer, you need to have it before the body of your web pageeven before any spaces.

 Adding Session Data

All your session data is stored in the session superglobal array, $_SESSION, which means that each session variable is one element in that array, combined with its value. Adding variables to this array is done in the same way as adding variables to any array, with the added bonus that session variables will still be there when your user browses to another page.
To set a session variable, use syntax like this:
    $_SESSION['var'] = $val;
    $_SESSION['FirstName'] = "Jim";

Older versions of PHP used the function session_register( ); however, use of this function is strongly discouraged, as it will not work properly in default installations of PHP 5. If you have scripts that use session_register( ), you should switch them over to using the $_SESSION superglobal, as it is more portable and easier to read.
Before you can add any variables to a session, you need to have already called the session_start( ) functiondon't forget!
 

$_ENV and $_SERVER ?

PHP sets several variables for you containing information about the server, the environment, and your visitor's request. These are stored in the superglobal arrays $_ENV and $_SERVER, but their availability depends on whether the script is being run through a web server or on the command line.

Useful preset variables in the $_SERVER superglobal
Name
Value
HTTP_REFERER
If the user clicked a link to get the current page, this will contain the URL of the previous page, or it will be empty if the user entered the URL directly.
HTTP_USER_AGENT
The name reported by the visitor's web browser.
PATH_INFO
Any data passed in the URL after the script name.
PHP_SELF
The name of the current script.
REQUEST_METHOD
Either GET or POST.
QUERY_STRING
Includes everything after the question mark in a GET request. Not available on the command line.


Of those, HTTP_REFERER and HTTP_USER_AGENT are the most important, as you can use these two to find out a lot about your visitor and then take the appropriate action. For example:
 
<?php
            if (isset($_SERVER['HTTP_REFERER'])) {
          print "previously was {$_SERVER['HTTP_REFERER']}
      <br />";
            } else {
              print "You didn't click any links to get here<br />";
            }
    ?>

    <a href="refer.php">Click me!</a>

If you load that page in your browser by typing the URL in by hand, the "You didn't click any links to get here" text is shown because HTTP_REFERER has not been set. However, if once the page is loaded you follow the "Click me!" link, the page will reload itself; this time, HTTP_REFERER will be set and the other message should appear. Although it can be easily spoofed, HTTP_REFERER is generally a good way to make sure a visitor came from a certain pagewhether you want to use that to say, "You can't download my files because you came from another site" or "Welcome, Google users!" is up to you.

Install Drupal

Now that you’ve created your database, go back into your favorite browser (I use
Chrome: http://www.google.com/chrome) and go to localhost:8888/d7-demo/install.
php. Choose the “standard” installation profile for now (see Figure 1-8); it will
take care of some basic configurations for you. On the next page, select English as the
installation language. If you need to install it in another language, there’s a handy link
on that screen that will show you how to do it.

Internal link architecture

Website linking architecture is important when it comes to SEO, especially when your
site has many pages or is continuously growing. To create a sound linking structure for
your site, you will likely need to partition your site into distinct subsections (or categories),
forming a uniform (inverted) treelike structure. Sometimes creating
subdomains can help in this regard. Other times, using XML Sitemaps can help showcase
your most important links.

Long-term content

If you use Google to search for any word that appears in the English dictionary, chances
are Google will provide among its search results a result from Wikipedia. The English
dictionary does not change much over time. Sites containing English language dictionary
definitions, such as Wikipedia, are well positioned to receive steady traffic over time.
Most computer programmers use Google to find answers to their development problems.
Sites providing answers to such problems can also benefit from this, as they will
receive a steady flow of traffic.


Creating custom website widgets

Everything starts with an idea. Creating custom widgets
can be as simple or as complex as required. You can create some website widgets by
simply creating custom HTML pages. Others you can create by utilizing Atom/RSS
feeds, Java applets, and Flash. In this section, we will create a sample HTML-based
widget to illustrate the concepts of link baiting and website widgets.
Let’s suppose you own a site that sells many different brands of alarm clocks. To promote
your site, you want to create your own link bait. Specifically, you want to create
a simple digital alarm clock widget (called Wake Up Service) that any site can link to
or use. When the widget is used on other sites, you specify the condition that your link
must be present to use the widget.
On your site, you also want to ensure that your link bait is highly visible (typically
placed at a strategic location for easy recognition).

Widget promotion and distribution.
You can proliferate your widgets in many ways. You can
do it from your site, you can use a third-party site, or you can employ both methods.
Make it easy for your visitors by offering simple cut-and-paste HTML code such as the
following:
<a href="http://scripts.seowarrior.net/quickpanel/wakeup.html"
target="_new">Wake Up Service</a>
This sample code shows the link to our Wake Up Service widget, which will open in a
new window. For website widgets such as the one we created in this example, you could
also write a small article that will be syndicated along with all of your other articles.
People subscribing to your feeds will be able to read about it and propagate the information
if they find it interesting.

Web server compression?

The best way to understand web server compression is to think of sending ZIP files
instead of uncompressed files from your web server to your web user. Sending less data
over the network will minimize network latency and your web users will get the file
faster.
The same thing applies to web spiders, as the major ones support HTTP 1.1. In fact,
search engines would appreciate the fact that they will need to use a lot less network
bandwidth to do the same work.
Web server compression is a technology used on the web server where you are hosting
your pages. If you have full control of the web server, you can set up this compression
to occur automatically for all websites or pages this server is hosting.
If you do not have this luxury, you can set this up in your code. To set up web server
compression in PHP, you can use the following PHP code:
<?php
ob_start("ob_gler");
?>
<HTML>
<body>
<p>This is the content of the compressed page.</p>
</body>
</HTML>
You can enable web server compression in your code in another way that is even easier
than the approach we just discussed. You can use the php.ini file that usually sits in
your root web folder. If it does not exist, you can create it. You can also place this file
in your subfolders to override the root php.ini settings.