This section describes some of the methods for generating dynamic content by using PHP.. We discuss the use of variables in generating content and how to use the environment, dates, and
Trang 1Chapter 2 Generating HTML
As Web sites become more and more user friendly, Web publishers want the content
to be more attractive and tailored to the tastes of each user Visit any of the top-tier Web sites today and each of them will offer some type of customization based on your preferences This customization might be in the form of personalized weather, stock quotes, or news Each of these preferences is read from a database based on your login ID or a cookie value that is passed in from your browser
This section describes some of the methods for generating dynamic content by using PHP We discuss the use of variables in generating content and how to use the environment, dates, and times to affect what the user sees We also discuss sending email from within PHP and using PHP to authenticate to a Web site
Generating Dynamic Content
This section defines dynamic content and then discusses practical ways to generate content The dynamic contents discussed in this section are limited to using request variables and the environment to generate content Other methods, namely using databases to generate content, will be discussed in the next chapter
Defining Dynamic Content
Let's first define dynamic content Dynamic content is where the user's Web
experience is determined by outside influences This means that content seen on a site in the morning, without intervention from the Webmaster, will change in the afternoon, and possibly again at night
Using Request Variables to Generate Content
PHP can use a number of variables to generate dynamic content Table 2.1 describes the environment variables that are available to the PHP script
Table 2.1 PHP Variables
Variable Description
argv argv is defined as an array of arguments passed to the script on
the command line This functionality gives a C-like look and feel
to command-line parameters When the PHP script is called using the GET method, argv will contain any query string information
passed to the script This adds a C-like look and feel to running the PHP scripts from the command line
allows the script to pass variables to itself based on input
HTTP_COOKIE_VARS HTTP_COOKIE_VARS is defined as an associative array of variables
passed to the PHP script containing the client's cookies
passed to the PHP script by a client using the HTTP GET method
Trang 2HTTP_POST_VARS HTTP_POST_VARS is defined as an associative array of variables
passed to the PHP script by a client using the HTTP POST method These variables are accessed through the use of PHP variables To illustrate how different types of information can be passed from one Web page to a PHP script, consider the following example:
<HTML>
<HEAD>
<TITLE>Posting Variables</TITLE>
<BODY>
<CENTER>Enter the following information:
<P>
<TABLE width="200"><TR><TD align="left">
<FORM ACTION="phpvariables.php?ITEM=10" METHOD="POST">
Your name:<BR>
<INPUT TYPE="text" NAME="name" SIZE="20" MAXLENGTH="30">
<P>
Your email address:<BR>
<INPUT TYPE="text" NAME="email" SIZE="20" MAXLENGTH="30">
<P>
I prefer:
<SELECT NAME="preference">
<OPTION value = Yankees>Yankees
<OPTION value = Braves>Braves
</SELECT>
<P>
<INPUT TYPE="submit" VALUE="Post Variables">
</FORM>
</TD></TR></TABLE></CENTER>
</BODY>
</HTML>
This file is a regular HTML page with no special qualities other than the FORM ACTION that calls a PHP script named phpvaribles.php with a variable passed in the query string portion of the URL The query string is any information after the question mark (?); in this case, ITEM=10
The second part of this example is the phpvariables.php script, which prints the information gathered in the form The script looks like this:
<?
echo "ITEM = $ITEM <br>";
echo "Email = $email <br>";
echo "Name = $name <br>";
echo "Preference = $preference <br>";
echo "argc = $argc <br>";
echo "argv = $argv[0] <br>";
echo "PHP_SELF = $PHP_SELF <br>";
phpinfo();
?>
This script reads the variables from the PHP environment and prints them out to the browser Your results should look something like this:
Trang 3ITEM = 10
Email = robertcox@anywhere.com
Name = Robert Cox
Preference = Braves
argc = 1
argv = ITEM=10
PHP_SELF = /phpbook/phpinfo.php
There should also be additional information at the bottom of this Web page generated from the phpinfo() function This function is useful to view the variables that are available for you to use For instance, under the Environment section of the
(Please note that not all the information output by phpinfo() is in this table.)
Table 2.2 PHP Environment Variables Displayed by phpinfo()
Variable >Valu
Program Files\ Exceed.nt\ ;
HTTP_ACCEPT_LANGUAGE en-us
AAMUILoginLocale
There is also a section that is output by the phpinfo() function that displays PHP variables This information differs somewhat from the environment variables detailed
in Table 2.2 Refer to Table 2.3 for this information
Table 2.3 PHP Variables Displayed by phpinfo()
Trang 4HTTP_POST_VARS["preference"] Braves
Using the Environment to Generate Content
One of the ways that you can vary content to the browser is to use the environment
to dictate what the user sees in the browser For instance, the information in the request variables could be used to alter the information that PHP displays In a similar manner, the date or time of day could be used to display greetings or other customized information to the user
In the following example, the input on the initial page determines what the user's experience will be on the resulting page This is a very elementary example, but it illustrates how the Web page can be modified on-the-fly to tailor content to the user
<HTML>
<HEAD>
<TITLE></TITLE>
<BODY>
<CENTER>Enter the following information:
<P>
<TABLE width="200"><TR><TD align="left">
<FORM ACTION="result.php" METHOD="POST">
Please select your gender:<BR>
<SELECT NAME="gender">
<OPTION value = Male>Male
<OPTION value = Female>Female
</SELECT>
<P>
<INPUT TYPE="submit" VALUE="Submit">
</FORM>
</TD></TR></TABLE></CENTER>
</BODY>
</HTML>
This is the initial page where the user, through the use of a drop-down list box, chooses his/her gender The resulting page will display a pink background if the chosen gender is female, or blue if the gender is male The text of the page will also reflect the choice that is made Here is the code for the result.php page:
<HTML>
<HEAD>
<TITLE>Result Page</TITLE>
<?
if($gender=="Male"){
$color = "Blue";
}
elseif($gender=="Female"){
$color = "Pink";
}
?>
Trang 5<BODY bgcolor=<? echo $color ?>>
<center><b>
<?
if($gender=="Male"){
echo "You selected Male";
}
elseif($gender=="Female"){
echo "You selected Female";
}
?>
</b></center>
</BODY>
</HTML>
This same technique can be used to change the content based on other variables detailed in Table 2.2 You can determine what browser a user has and alter the content accordingly or you could read the time of the PHP server by using the date() function and modify the content based on what time the user hits the site These are just a few examples of how to use the PHP environment to tailor content to the user The limit to this customization is up to you and your imagination
Working with the Filesystem
Sometimes you might find it necessary to read the host filesystem to determine rights to directories, to display files, to write to files, or any number of things PHP provides many filesystem functions that enable the developer to read, delete, and modify any number of attributes on directories or files Please remember that when using these functions, the Web server oftentimes runs as a specific user with limited rights All the filesystem operations discussed will return information based on the user as which PHP (or the Web server) is running This means that if you try to create a file and you don't have the appropriate rights, you will receive an error Please keep this in mind as you work with files and directories
Directory Functions
When working with directory functions such as readdir() rewinddir(), the basic idea is threefold First, you must open a directory handle by using the opendir() function This handle is a reference that is used in subsequent directory operations Second, you perform operations on the directory These could be anything from renaming the directory to changing permissions Third, you close the directory handle by using the closedir() function Some directory functions do not require the use of a directory handle For functions such as is_dir(), mkdir(), and rmdir(), only the directory name is required The following example tests for the existence of the current directory Of course, this will always return true You can change the $dirname variable and experiment with what happens if the is_dir() function returns false
<html>
<head>
<title>Testing for a Directory</title>
</head>
<body>