Sunday, June 3, 2012

PHP Multipurpose


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:

We need the name on every page of the site, not just one.

We have no control over which page of our site users will view first



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:2
<a href="newpage.php?name=<?php echo urlencode($_GET['name']);?>"> A link</a>


Notice that we’ve embedded PHP code right in the middle of an HTML tag. This is perfectly legal, and will work just fine.

You’re familiar with echo statements, but the urlencode function is probably new to you. This function takes special characters in the string (for example, spaces) and converts them into the special codes they need to be in order to appear in the query string. For example, if the $name variable had a value of 'Kevin Yank', then, as spaces are not allowed in the query string, the output of urlencode (and thus, the string output by echo) would be 'Kevin+Yank'. PHP would then convert it back automatically when it created the $_GET variable in newpage.php. Okay, so the user’s name will be 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 containing a form 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 Website 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.3 This means that every page of our site will either display its content, or prompt the user to enter a name, depending on whether the $name variable is found to have a value. If you think this is beginning to sound like a good place for an if-else statement, you’re a quick study!

We’ll refer to pages that can decide whether to display one thing or another as multipurpose pages. The code of a multipurpose page looks something like this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Multipurpose Page Outline</title>
<meta http-equiv="content-type"
content="text/html; charset=iso-8859-1" />
</head>
<body>
<?php if (condition) { ?>
<!-- HTML content to display if condition is true -->
<?php } else { ?>
<!-- HTML content to display if condition is false -->
<?php } ?>
</body>
</html>


This code may confuse you at first, but, in fact, this is just a normal if-else statement with HTML code sections that depend on the condition, instead of PHP statements. This example illustrates one of the big selling points of PHP: that you can switch in and out of “PHP mode” whenever you like. If you think of <?php as the command to switch into “PHP mode”, and ?> as the command to go back into “normal HTML mode,” the above example should make perfect sense.

There’s an alternate form of the if-else statement that can make your code more readable in situations like this. Here’s the outline for a multipurpose page using the alternate if-else form:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Multipurpose Page Outline</title>
<meta http-equiv="content-type"
content="text/html; charset=iso-8859-1" />
</head>
<body>
<?php if (condition): ?>

<!-- HTML content to display if condition is true -->
<?php else: ?>
<!-- HTML content to display if condition is false -->
<?php endif; ?>
</body>
</html>

Okay, now that we have all the tools we need in hand, let’s look at a sample page of our site:
File: samplepage.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Sample Page</title>
<meta http-equiv="content-type"
content="text/html; charset=iso-8859-1" />
</head>
<body>
<?php if (!isset($_GET['name'])): ?>
<!-- No name has been provided, so we
prompt the user for one. -->
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="get">
<label>Please enter your name:
<input type="text" name="name" /></label>
<input type="submit" value="GO" />
</form>
<?php else: ?>
<p>Your name: <?php echo $_GET['name']; ?></p>
<p>This paragraph contains a
<a href="newpage.php?name=<?php echo urlencode($_GET['name']);
?>">link</a> that passes the name variable on to the next
document.</p>
<?php endif; ?>

</body>
</html>
There are two new tricks in the above code, but overall you should be fairly 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 in this example), and false if the variable does not exist (i.e. if a name has not yet been provided). The exclamation mark (also known as the negation operator, or the not operator), which 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 $_GET['name'] variable is not set.

The second new trick is the use of the variable $_SERVER['PHP_SELF'] to specify the action attribute of the <form> tag. Like $_GET, $_POST, and $_REQUEST, $_SERVER is an array variable that is automatically created by PHP. $_SERVER contains a whole bunch of information supplied by your Web server. In particular, $_SERVER['PHP_SELF'] will always be set to the URL of the current page. This gives us an easy way to create a form that, when submitted, will load the very same page, but this time with the $name variable specified. 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, as shown in Figure 3.8. Once they enter their names and click GO, they’ll be presented with the exact page they requested.

By PHP with No comments

0 comments:

Post a Comment

    • Popular
    • Categories
    • Archives