Home >Backend Development >PHP Tutorial >Other functions_PHP tutorial
Reprinted from php Chinese user 5. Other miscellaneous items
5.1 Generate images
PHP can operate and process images. If you have the GD library installed, you can even generate images using PHP.
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’s Note: The above code snippet lacks comments, please refer to the image processing function section of the PHP Manual)
This code is called in other pages through the following tag , and then the above The button.php3 code section obtains the text value and adds the value to the image file obtained separately - in the above code, the image file is images/button1.gif - and finally outputs it to the browser. If you want to use an image button in a form field, but don't want to have to regenerate a new image every time the text on the button changes, you can use this simple method to dynamically generate an image file.
5.2 Cookies
PHP supports HTTP-based cookies. When needed, you can use cookies just as easily as regular variables. Cookies are pieces of information that the browser saves on the client, so you can know whether anyone on a specific PC has visited your site, the visitor's traces on your site, and so on. A typical example of using cookies is the screening of browser preferences. Cookies are set by the function setcookie(). Like the function header() that outputs HTTP headers, setcookie() must be called before any actual content is output to the browser. The following is a simple example:
if (empty($VisitedBefore))
{
// If the cookie is not set, assign the current time value to the cookie
// The last parameter in the function declares the time the cookie will be saved
// In this example it is 1 year
// The time() function returns the time in seconds since January 1, 1970
SetCookie("VisitedBefore",time(), time()+(60*60*24*365));
}
else
{
// Visitors are welcome to visit again
echo "Hello there, welcome back
";
// Read the cookie and determine
if ( (time() - $VisitedBefore) >= "(60*60*24*7 )" )
echo "Why did you take a week to come back. You should be here more often!? ";
}
?>
5.3 Based on HTTP verification
HTTP-based authentication is not possible when PHP is running in CGI mode. We can use the function header() to send an HTTP header to force authentication, and the client browser will pop up a dialog box for entering the username and password. These two variables are stored in $PHP_AUTH_USER and $PHP_AUTH_PW. You can use these two variables to verify legality and allow entry. The following example authenticates a user's login for tnc/nature using a username/password pair:
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 buttonn";
exit;
}
else
{
if ( !($PHP_AUTH_USER=="tnc" && $PHP_AUTH_PW=="nature") )
{
// If it is the wrong username/password Yes, 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 more practical References are unlikely to use explicit username/password pairs like the snippet above, but instead use a database or encrypted password file to access them.
5.4 File upload
You can use PHP to implement the file function. Note that the client browser should be Netscape3 or above or IE3 or above.以下就是该功能的简单演示:
( upload.html ):
| n";
while($entry = $d->read())
{
// 忽略无文件情况
if ( !is_file($entry) )
continue;
/* 将文件名与扩展名分开。Since . is a special character in regular expressions, it should be introduced using */
list($filenm, $fileext) = split(".",$entry, 2);
// Ignore non-.php3 files
if( $fileext != "php3" )
continue;
/* Now that we have selected all the .php3 files, let’s search for the first line (title) in the file
similar to $title ="something";
And separate the above title content and use it as link text*/
$linknm = "";
$fp=fopen($entry,"r");
while ($buffer=fgets($fp, 4096))
{
$buffer = trim($buffer);
// We have put the title of each file on the first line of the file for easy search
// But it may cause trouble when you change the variable name
if (ereg("title *= *"", $buffer))
{
/* We have achieved The title content can be processed based on
to remove spaces.
must be processed in PHP code, such as $title = "blah blah" */
eval($buffer);
/ / Then display the link text as title text
$linknm = $title;
break;
}
}
fclose($fp);
if ( $entry == basename ($PHP_SELF) )
echo "$linknm";
else
echo "$linknm";
echo " | ";
}
$d->close();
echo "
";
exit;
}
}
if ( $cancelit )
{
// When the visitor presses the "Cancel" button, it will redirect to the home 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 did not 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 use this Check file name/type pairs to determine what kind of file it is: 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
?>
Here are some of our family photos. This PHP script can really
be made better, by splitting into multiple pages.