Multi-Purpose Pages - Page 5
January 17, 2002
Let's say you wanted to construct your site so that it showed the visitor's name at the top of every page. With our custom welcome message example above, we're halfway there already. Here are the problems we'll need to overcome to extend the example into what we need:
The first problem isn't too hard to overcome. Once we have the user's name in a variable on one page, we can pass it with any request to another page by adding the name to the query string of all links:
Notice that we've embedded PHP code right in the middle of an HTML tag. This is perfectly legal, and will work just fine. A shortcut exists for those times when you simply want to echo a PHP value in the middle of your HTML code. The shortcut looks like this:
You're familiar with the
Okay, so we've got the user's name being passed with every link in our site. Now all we need is to get that name in the first place. In our welcome message example, we had a special HTML page with a form in it that prompted the user for his or her name. The problem with this (identified by the second point above) is that we couldn't — nor would we wish to — force the user to enter our Web site by that page every time he or she visited our site.
The solution is to have every page of our site check to see if a
name has been specified, and prompt the user for a name if
necessary. This means that every page of our site will either
display its content, or a prompt the user to enter a name,
depending on whether the
We'll refer to pages that are capable of displaying completely different content depending on some condition, as "multi-purpose pages". The code of a multi-purpose page looks something like this:
This may confuse you at first, but in fact this is just a normal
There's an alternate form of the
Okay, now that we have all the tools we need in hand, let's look
at a sample page of our site (
There are two new tricks in the above code, but overall you
should be pretty comfortable with the way it works. First of all,
we're using a new function called isset in the condition.
This function returns (outputs) a value of true if the variable
it is given has been assigned a value (i.e. if a name has been
provided), and false if the variable does not exist (i.e. if a
name has not yet been given). The exclamation mark (also known as
the negation operator, or the not operator) that
appears before the name of the function reverses the returned
value from true to false or vice-versa. Thus, the form is
displayed when the
If we structure all the pages on our site in this way, visitors will be prompted for their name by the first page they attempt to view, whichever page this happens to be. Once they enter their name and click "GO", they'll be presented with the exact page they requested. The name they entered is then passed in the query string of every link from that point onward, ensuring that they are prompted only the once.
In this chapter, we've had a taste of the PHP server-side scripting language by exploring all the basic language features: statements, variables, operators, and control structures. The sample applications we've seen have been pretty simple, but don't let that dissuade you. The real power of PHP is in the hundreds of built-in functions that let you do everything: access data in a MySQL database, send e-mail, dynamically generate images, and even create Adobe Acrobat PDF files on the fly.