PHP newbies on the road (14)_PHP tutorial

WBOY
Release: 2016-07-21 16:01:28
Original
791 people have browsed it

Other miscellaneous items

13.1 Generating 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);
?>

(translation Editor's note: The above code snippet lacks comments, please refer to the image processing function section of the PHP Manual)

This code is marked as follows in other pages: Call, and then the above button.php3 code 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.

13.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 is saved
// In this example it is 1 year
// The time() function returns the number of seconds since January 1, 1970 Calculated time
SetCookie("VisitedBefore",time(), time()+(60*60*24*365));
}
else
{
// Welcome to browse The visitor comes again
echo "Hello there, welcome back
";
// Read the cookie and judge
if ( (time() - $VisitedBefore) >= "(60*60* 24*7)" )
echo "Why did you take a week to come back. You should be here more often!? ";
}
?>

13.3 Common functions

Let’s take a brief look at some commonly used functions.

Array

array - Generate array
count - Number of array elements
sort - Array sorting, there are several other sorting functions available
list - Column Out the array element
each - Returns the next key/value pair
current - Returns the current array element
next,prev - Returns the pointers before and after the current array element



Date and time

checkdate - Verify date/time format
date - Generate date/time format
time - Current time information
strftime - Format date/time

Directory, file system

chdir - change directory
dir - directory category
opendir, readdir, closedir - open, read, close directory
fopen, fclose - open, close file
fgets, fgetss - Read content line by line
file - Read the entire file into an array variable

Regular expression

ereg - Match regular expression
eregi - Case-insensitive matching regular expressions
ereg_replace - Matches regular expressions and replaces them
eregi_replace - Case-insensitive matching regular expressions and replaces them
split - Splits strings according to rules and stores them in array form



String

AddSlashes - Use a string after adding a slash
echo - Output one or more strings
join, implode - Will Array elements are combined into strings
htmlentities, htmlspecialchars - Convert HTML special characters into HTML tag form
split - Split the string according to rules and store it in array form


13.4 Extend us Sample homepage

We will use some of the functions and ideas mentioned above to add more dynamic content to our sample homepage. We can add a navigation bar at the top of each page, and at the same time make the current page automatically not displayed by links; we can also add a user verification form to upload music, images and other files and automatically update the page.

Navigation bar

Actually just add a piece of code to the footer.inc file.Assume that all files with the suffix .php3 in your website will appear in the navigation bar. The following is the code saved as include/navbar.inc:

/* Output the navigation column, link all .php3 files in the site except the current page*/
# Read directory
$d = dir("./");
echo "

| n";
while($entry = $d->read())
{
// Ignore no file situation
if ( !is_file($entry) )
continue ;
/* Separate filename from extension. Since . is a special character in regular expressions, it should be quoted */
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 "

n";
?>

Photo Favorites

We will use 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 can list all the photos in the directory.

File upload

include("include/common.inc");
// We do another user verification here
if(! isset($PHP_AUTH_USER))
{
Header("WWW-Authenticate: Basic realm="$MySiteName"");
Header("HTTP/1.0 401 Unauthorized");
echo "Sorry , you are not authorized to upload filesn";
exit;
} 
else 
{
if ( !($PHP_AUTH_USER==$MyName && $PHP_AUTH_PW==$MyPassword ) )
{
// If the username/password pair is wrong, 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 visitor presses the "Cancel" button, it will redirect to the homepage
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 also File name/type pairs can be checked 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
?>
< FORM ACTION="" ENCTYPE="multipart/form-data" METHOD=POST>









(You may 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 are 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 and displayed by the code in the photo gallery above.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/316820.htmlTechArticleOther miscellaneous 13.1 Generating 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=implo...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template