5. Other Miscellaneous
5.1 Generating Images
PHP can manipulate image processing. If you have installed the GD library, you can even use PHP to generate images.
Header ("Content-type:image/gif");
$string =implode ($ARGV, "");
$im = Imagecreatefromgif ("Images/button1.gif");
$orange = Imagecolorallocate ($im, 220, 210, 60);
$PX = (Imagesx ($im) -7.5*strlen ($string))/2;
Imagestring ($im, 3, $px, 9, $string, $orange);
Imagegif ($im);
Imagedestroy ($im);
?>
(Translator Note: The above code snippet is missing comments, please refer to the Image Processing function section of PHP manual)
This code is called by the following tag on other pages, and then the BUTTON.PHP3 code above gets the text value and adds the value to the additional image file-in the above code, the image file is images/button1.gif--last output to the browser. If you want to use the image button in the form field, but do not want to have to regenerate the new image after the text on each button has changed, you can use this simple method to generate the image file dynamically.
5.2 Cookies
PHP supports HTTP-based cookies. You can use cookies as easily as you would if you were using a generic variable. Cookies are some pieces of information that a browser saves on the client, so you can tell if anyone on a particular PC has visited your site, the whereabouts of the viewer on your site, and so on. A typical example of using cookies is the screening of the preferences of the visitors. Cookies are set by the function Setcookie (). As with the function header () of the output HTTP header, Setcookie () must be called before any actual content cups are output to the browser. Here is a simple example:
if (empty ($VisitedBefore))
{
If no cookie is set, the current time value is assigned to the cookie
The last parameter in the function declares the time that the cookie was saved
In this case, it's 1 years.
Time () function returns the number of seconds since January 1, 1970
Setcookie ("Visitedbefore", Time (), Time () + (60*60*24*365));
}
Else
{
Welcome visitors to visit again
echo "Hello there, welcome back
";
Read the cookie and judge
if (Time ()-$VisitedBefore) >= "(60*60*24*7)")
echo "Why do you take a week to come back." You should is here more often!?
";
}
?>
5.3 HTTP-based authentication
HTTP-based authentication is not possible when PHP is running in CGI mode. We can use the function header () to send HTTP headers to force validation, and the client browser pops up a dialog box for entering the user name and password. These two variables are stored in $php_auth_user and $PHP_AUTH_PW, and you can use these two variables to verify the legitimacy and allow access. The following example verifies a user's login with a user name/password pair for tnc/nature:
if (!isset ($PHP _auth_user))
{
Header ("Www-authenticate:basic realm=\" "My realm\");
Header ("http/1.0 401 Unauthorized");
echo "Text to send if user hits Cancel button\n";
Exit
}
Else
{
if (! ( $PHP _auth_user== "TNC" && $PHP _auth_pw== "nature")
)
{
If the user name/password pair is incorrect, force re-authentication
Header ("Www-authenticate:basic realm=\" "My realm\");
Header ("http/1.0 401 Unauthorized");
echo "ERROR: $PHP _auth_user/$PHP _AUTH_PW is invalid.";
Exit
}
Else
{
echo "Welcome tnc!";
}
?>
In fact, it is unlikely that the actual references will be used to access them using either a database or an encrypted password file, as shown above with the explicit user name/password pair of the code snippet.
5.4 File Upload
You can use PHP to implement the function of the file, note that the client's browser should be Netscape3 or above or IE3. Here is a simple demonstration of this feature:
(upload.html):
<title>Upload Your File</title>
(Notice a slight
Delay while we upload your file.)
Here are the files that are processed for uploading:
(RECEIVER.PHP3):
function Do_upload ()
{
Global $uploadfile, $uploadfile _size;
Global $local _file, $error _msg;
if ($uploadfile = = "None")
{
$error _msg = "You do not have specify a file for uploading.";
Return
}
if ($uploadfile _size > 2000000)
{
$error _msg = "Sorry, your file is too large.";
Return
}
$the _time = time ();
You need to have write access to the following directories
$upload _dir = "/local/uploads";
$local _file = "$upload _dir/$the _time";
if (file_exists (' $local _file '))
{
$seq = 1;
while (File_exists ("$upload _dir/$the _time$seq")) {$seq + +;}
$local _file = "$upload _dir/$the _time$seq";
};
Rename ($uploadfile, $local _file);
Display_page ();
}
function Display_page ()
{
Here is the content of your page
}
<title>PhP3 receiving Script</title>
if ($error _msg) {echo]
$error _msg
";
}
if ($sendit)
{
Do_upload ();
}
ElseIf ($cancelit)
{
Header ("Location: $some _other_script");
Exit
}
Else
{
Some_other_func ();
}
?>
5.5 Common functions
Let's take a look at some commonly used functions.
Array
Array-generating arrays
Count-Number of array elements
Sort-array sort, with several other sort functions available
List-list array elements
Each-returns the next Key/value pair
Current-Returns the currently array element
Next,prev-Returns the current array element before and after the pointer
Date and time
Checkdate-Verify Date/Time format
Date-generates a day/time format
Time-Current hour information
Strftime-formatted date/time
Directory, File system
ChDir-Change Directory
DIR-Catalog Category
Opendir, Readdir, Closedir-open, read, close directory
fopen, fclose-Open, close file
Fgets, FGETSS-read content line by row
File-Reads the entire file into an array variable
Regular expressions
Ereg-Match Regular expression
Eregi-casing non-sensitive matching regular expression
Ereg_replace-matches regular expressions and replaces
Eregi_replace-casing non-sensitive match regular expression and replace
Split-cut strings according to rules and store them in an array of situations
String
Addslashes-Use a string after adding a slash
echo-outputs one or more strings
Join, implode-merges array elements into strings
Htmlentities, Htmlspecialchars-Convert HTML special characters to HTML markup form
Split-cut strings according to rules and store them in an array of situations
5.6 Extending our Example home page
We will use some of the above mentioned functions and ideas to add more dynamic content to our example homepage. We can add a navigation bar at the top of each page, while the current page is automatically not displayed, and a user verification form can be added to upload music, images and other files and automatically update the page.
Navigation bar
is actually adding a piece of code to the Footer.inc file. Assume that all files in your Web site that have a. php3 file will appear in the navigation bar, and the following is the code saved as Include/navbar.inc:
/* Output the navigation bar to link all stations except the current page. php3 file */
# Read Directory
$d = Dir ("./");
echo "
| \ n ";
while ($entry = $d->read ())
{
Ignore no file conditions
if (!is_file ($entry))
Continue
/* Separate the file name from the extension. Because. is a regular expression special character, you should use \ Out */
List ($filenm, $fileext) = Split ("\.", $entry, 2);
Ignore non. php3 file conditions
if ($fileext! = "PhP3")
Continue
/* Now that we have selected the. php3 file, search for the first line (title) in the file below
Similar to $title= "something";
and separate the above headings to use as link text */
$LINKNM = "";
$FP =fopen ($entry, "R");
while ($buffer =fgets ($fp, 4096))
{
$buffer = Trim ($buffer);
We have placed the title of each file in the first line of the file in order to search
But it can be annoying when you change the name of a variable.
if (Ereg ("title *= *\", $buffer))
{
/* We've got the headline content and can build on it
The removal of whitespace and other processing.
Must be handled in PHP code, such as $title = "blah blah" */
eval ($buffer);
Then display the linked text as the title text
$LINKNM = $title;
Break
}
}
Fclose ($FP);
if ($entry = = basename ($PHP _self))
echo "$LINKNM";
Else
echo "$LINKNM";
echo "|";
}
$d->close ();
echo "
\ n ";
?>
Photo Collection
We will refer to HTTP-based authentication, file system functions, and file upload functions to maintain the directory where the image files are placed.
At the same time we need to create a page that lists all the photos in that directory.
File Upload
Include ("Include/common.inc");
We'll do it again. User authentication
if (!isset ($PHP _auth_user))
{
Header ("Www-authenticate:basic realm=\" $MySiteName \ "");
Header ("http/1.0 401 Unauthorized");
echo "Sorry, you is not authorized to upload files\n";
Exit
}
Else
{
if (! ( $PHP _auth_user== $MyName && $PHP _auth_pw== $MyPassword))
{
If it is the wrong user name/password pair, Force re-authentication
Header ("Www-authenticate:basic realm=\" "My realm\");
Header ("http/1.0 401 Unauthorized");
echo "ERROR: $PHP _auth_user/$PHP _AUTH_PW is invalid.
";
Exit
}
}
if ($cancelit)
{
When the browser presses the Cancel button, it turns to the first page.
Header ("Location:front_2.php3");
Exit
}
function Do_upload () {
Global $userfile, $userfile _size, $userfile _name, $userfile _type;
Global $local _file, $error _msg;
Global $HTTP _referer;
if ($userfile = = "None") {
$error _msg = "You do not have specify a file for uploading.";
Return
}
if ($userfile _size > 2000000)
{
$error _msg = "Sorry, your file is too large.";
Return
}
Wherever you have write permission below ...
$upload _dir = "Photos";
$local _file = "$upload _dir/$userfile _name";
if (file_exists ($local _file)) {
$error _msg = "Sorry, a file with that name already exists";
Return
};
You can also check the file name/type pair to determine what kind of file: Gif,jpg,mp3 ...
Rename ($userfile, $local _file);
echo "The file is uploaded
\ n ";
echo "Go back
\ n ";
}
$title = "Upload File";
Include ("Include/header.inc");
if (Empty ($userfile)
$userfile = = "None")
{
Output the following form
?>
(Notice a slight delay while
We upload your file.)
} else {
if ($error _msg) {echo]$error _msg
";
}
if ($sendit) {
Do_upload ();
}
}
Include ("Include/footer.inc");
?>
Photo Gallery
Include ("Include/common.inc");
$title = "Gallery";
Include ("Include/header.inc");
?>
Here is some of our family photos. This PHP script can really
be made better, by splitting into multiple pages.
$d = Dir ("photos");
while ($entry = $d->read ())
{
if (Is_file ("photos/$entry"))
echo "\ n";
}
$d->close ();
?>
Include ("Include/footer.inc");
?>
In addition, you can add an INPUT element to the file upload form to describe the uploaded file. This element will be stored in the file and then read out and displayed by that piece of code in the photo gallery above.
http://www.bkjia.com/PHPjc/314690.html www.bkjia.com true http://www.bkjia.com/PHPjc/314690.html techarticle 5. Other miscellaneous 5.1 Generate images PHP can manipulate images. If you have installed the GD library, you can even use PHP to generate images. Header ("Content-type:image/gif"); $string =imp ...