A simple counter _ PHP Tutorial written in php3

Source: Internet
Author: User
A simple counter written in php3. Php has extremely powerful image processing capabilities and can be used to dynamically generate web images easily. Here is a simple counter made of php. 1. overall thinking: using php with extremely powerful image processing capabilities, it can easily generate web images dynamically.
Here is a simple counter made of php.

1. overall thinking:
Records the number of previous visitors in a text file. when a webpage is accessed
Read the previous number of visitors, add 1 to get the latest number of visitors, and format the number
The standard format is used to call the image processing function, output the number as an image, and then return the new access number.
It is written to the file that records the number of visitors.

2. function description used by the program:
A. related file operations:
A. open the file:
Function prototype: int fopen (string filename, string mode );
Returned result: if the file is successfully opened, the function returns the file stream pointer; otherwise, FALSE (0) is returned ).
Parameter description:
String filename -- Name of the file to be opened, which must be in the string format.
For example, "zzm.txt" and ".. \ zzm.txt.
String mode: the method to open a file. it must be a character.
'R' in read-only mode. the file pointer points to the beginning of the file.
'R + ', readable and writable. the file pointer points to the beginning of the file.
'W', write-only, the file pointer points to the beginning of the file, cut the file length to 0,
If the file does not exist, create a file.
'W + ', readable and writable. the file pointer points to the beginning of the file and cut the file length to 0,
If the file does not exist, create a file.
'A', append form (only writable), the file pointer points to the end of the file, if
File does not exist.
'A + ', readable and writable. the file pointer points to the end of the file. if the file does not exist,
Will try to create a file.
Example: open "zzm.txt" in the current directory in read-only mode"
$ Fp = fopen ("zzm.txt", "r ");

B. close the file:
Function prototype: int fclose (int fp );
Returned results: 1 is returned for success, and 0 is returned for failure.
Parameter description: int fp is the file stream pointer returned by the fopen function.
Example: use fopento open the zzm.txt file.
Fclose ($ fp );

C. Read files:
Function prototype: string fgets (int fp, int length );
Return result: a string Of length-1 is returned. if the string ends at the End Of the File, EOF (End Of File) is returned)

Parameter description:
Int fp-the file stream pointer to read data, which is a value returned by the fopen function.
Int length -- number of characters read. the actual number of characters read is length-1.
Example: Read 9 characters from $ fp
$ Str1 = fgets ($ fp, 10 );

D. write a file:
Function prototype: int fputs (int fp, string str, int [length]);
Returned results: same as fclose
Parameter description:
Int fp-the file stream pointer to write information, which is a value returned by the fopen function.
String str -- string of the file to be written.
Int length -- the write length. optional. if length is not provided, the entire string is written,
Otherwise, write length characters.
Example: write "0000000001" to $ fp"
Fput ($ fp, "0000000001 ");

B. related string functions:
A. calculate the string length:
Function prototype: int strlen (string str );
Return result: returns the length of the string.
Parameter description:
String str -- string to calculate the length
Example: calculate the length of the string "000000000"
$ Str2 = "000000000 ";
$ Len2 = strlen ($ str );

B. string addition: it is simplest. use one to connect the two strings.
Example: add $ str1 and $ str2
$ Str = $ str1. $ str2

C. related graphic functions:
A. create an image:
Function prototype: int imagecreate (int x_size, int y_size );
Return result: returns an empty image ID (ImageID) x y pixels)
Parameter description: x_size and y_size indicate the width and height of the newly created image (in pixels)
Example: create an empty 88*31 pixel image
$ ImageID = imagecreate (88, 31 );

B. assign a color to the image:
Function prototype: int imagecolorallocate (int im, int red, int green, int blue );
Return result: returns an RGB color identification number to the image ($ im ).
Description of parameters: int im image identification number
Int red, green, and blue are the components of red, green, and blue respectively. The value ranges from 0 to 255.
Example: assign a identification number $ im to the image, which is $ white in white color and RGB (255,255,255) in white color)
$ White = imagecolorallocate ($ im, 255,255,255 );
C. fill the image with color:
Function prototype: int imagefill (int im, int x, int y, int col );
Return result: 1 is returned successfully; otherwise, 0 is returned.
Parameter description: int im, image identification number
Int x, int y, fill the color from the (x, y) coordinates of the image
(0, 0) indicates the upper left corner of the image
Int col, the identification number of the color
Example: fill in black from the upper left corner of the image (that is, the entire image) (the imagecolorallocate function has been used.
Defines the black color identification number as $ black ).
Imagefill ($ im, 0, 0, $ black );

D. calculate the image width:
Function prototype: int imagesx (int im );
Returned result: the width of the returned image, in pixels)
Parameter description: int im, the identification number of the image.
Example: calculate the width of the image $ im
$ Px = imagesx ($ im );

E. write horizontal text in the image:
Function prototype: int imagestring (int im, int font, int x, int y, string s, int col)

Return result: 1 is returned successfully; otherwise, 0 is returned.
Parameter description: int im, image identification number
Int font, font identification number, built-in font 1 to 5, you can use imageloadfont () yourself?
?
Font.
Int x, int y, starts to write the coordinates of the font, (0, 0) is the upper left corner of the image.
String s, the string to be written
Int col, the color identification number of the font
Example: write a font size of 3 at the image (3, 3) and a white color (imagecolorallocate () function has been used.
Defines the string "E & J Counter" for the black color identification number $ white"
ImageString ($ im, 3, 3, 3, "E & J Counter", $ white );

F. draw a straight line in the image:
Function prototype: int imageline (int im, int x1, int y1, int x2, int y2, int col );
Return result: 1 is returned successfully; otherwise, 0 is returned.
Parameter description: int im, image identification number
Int x1, int y1, the starting coordinate of the dash
Int x2, int y2, the ending coordinate of the dash
Int col, line color identification number
Example: draw a straight line from () to () in the image $ im with a color of $ white
Imageline ($ im, 1, 14, 85, 14, $ white );

G. output the image into GIF format:
Function prototype: int imagegif (int im, string filename );
Return result: 1 is returned successfully; otherwise, 0 is returned.
Parameter description: int im, image identification number
String filename: the name of the image to be generated. optional. if filename is empty, it is used directly?
Neon?
Image, you need to use Header ("Content-type: image/gif") to pre-define
For images
Example: output image $ im into an image named "image1.gif"
Imagegif ($ im, "image1.gif ");

H. release the image:
Function prototype: int imagedestroy (int im );
Return result: 1 is returned successfully; otherwise, 0 is returned.
Parameter description: int im, the ID of the image to be released. This function releases the image and image occupied by the identification number im.
System resources used.
Example: release an image $ im
Imagedestroy ($ im );

3. how to install this counter:
A. The PHP interpreter must be installed in the system. PHP can be downloaded at http://www.php.net /.
Details
You can browse or download the technical materials. For instructions on how to install PHP, see.
B. Copy the following program list to a file, and set the extension to php to the directory where the php script can be run?
Travel agency?
Create a file named zzm.txt in the directory. This file is used to record previous
Visitor
Number. You can set the initial counter value in advance, for example, 5000.
C. How can I call this counter on a webpage? You can call the following method:



Appendix: complete program list
Header ("Content-type: image/gif ");
// Define output as image type

$ Fp = fopen ("zzm.txt", "r ");
// Open the log in the form of reading the file zzm.txt

$ Str1 = fgets ($ fp, 10 );
// Read 9 characters from the file. The maximum number of visitors recorded by this counter is 999999999.

$ Str1 ++;
// Counter addition

Fclose ($ fp );
// Close the file

$ Fp = fopen ("zzm.txt", "w ");
// Write a document to record the number of people in zzm.txt

Fputs ($ fp, $ str1 );
// Write the latest number of visitors to the file

Fclose ($ fp );
// Close the file

/*
The following is a formatted output of the number of visitors. if the number of visitors is less than 9, for example, 5000 (4 ),
The number of visitors is converted to 000005000. The method is to calculate the number of visits and
Compare it with the number of digits (9 bits) of 000000000 to obtain the number of digits with a difference.
0. For example, the length of 5000 is 5 different from that of 000000000, so 5 zeros must be added before 5000.
*/

$ Len1 = strlen ($ str1 );
// Calculate the number of visits

$ Str2 = "000000000 ";
$ Len2 = strlen ($ str );
// Define the maximum counter count

$ Dif = $ len2-$ len1;
// Calculate the difference between the two digits, that is, the number of zeros to be supplemented.

$ Rest = substr ($ str2, 0, $ dif );
// Intercept the 0 to be supplemented

$ String = $ rest. $ str1;
// Add 0 in front

$ Font = 4;
// Define the font size

$ Im = imagecreate (88,31 );
// Create an image

$ Black = ImageColorAllocate ($ im, 0, 0 );
// Define Black

$ White = ImageColorAllocate ($ im, 255,255,255 );
// Define White

Imagefill ($ im, 0, 0, $ black );
// Set the background color of the counter to black.

$ Px = (imagesx ($ im)-8.3 * strlen ($ string)/2;
// Calculate the horizontal coordinates of the string to be written based on the length of the string. the objective is to make the string horizontal align as much as possible.

ImageString ($ im, 3, $ px, 2, "E & J Counter", $ white );
// Write "E & J Counter" to the image"

Imageline ($ im, 1, 14, 85, 14, $ white );
// Draw a horizontal line

ImageString ($ im, $ font, $ px, 15.5, $ string, $ white );
// Write access count

ImageGif ($ im );
// Output the image to GIF format
ImageDestroy ($ im );
// Release the image
?>

Bytes. Here is a simple counter made of php. 1. overall thinking: put the previous...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.