A simple counter written in php3 _php tutorial

Source: Internet
Author: User
Tags function prototype truncated
PHP has an extremely powerful image processing capability, which makes it easy to dynamically generate web images.
is a simple counter made using PHP.

1. General ideas:
Record the number of previous visits in a text file, and when the page is accessed, open the file from
And read out the past number of visitors, add 1, get the latest number of visitors, and format the number of
The standard format, and then call the image processing function, the output of the number into a picture, and then put the new access number back
Written in a document that records the number of visitors.

2. Description of functions used by the program:
A. Related file operations:
A. Open the file:
The prototype of the function: int fopen (string filename, string mode);
Returns the result: if the open file succeeds, the function returns a file stream pointer, otherwise FALSE (0) is returned.
Parameter description:
String filename--The name of the file to be opened must be in the form of a string.
For example, "Zzm.txt", ". \zzm.txt "and so on.
String mode-The way the file is opened must be in the form of a character.
' R ', read-only, the file pointer points to the beginning of the file
' r+ ', readable and writable, pointer to the beginning of the file
' W ', write-only form, file pointer to the beginning of the file, the length of the file truncated to 0,
If the file does not exist, it will attempt to create a file.
' w+ ', readable and writable, the file pointer points to the beginning of the file, the length of the file truncated to 0,
If the file does not exist, it will attempt to create a file.
' A ', append form (only writable), file pointer to the end of the file, if the text
The file does not exist and will attempt to build it.
' A + ', readable and writable, the file pointer pointing to the end of the file, if the file does not exist,
Will attempt to build the file.
Example: Open "Zzm.txt" below the current directory in read-only form
$fp = fopen ("Zzm.txt", "R");

B. Close the file:
Function prototype: int fclose (int fp);
Return result: Successfully returned 1, failure returned 0
Parameter description: The int FP is a file stream pointer returned by the fopen function.
Example: Close the Zzm.txt file that you just opened with fopen
Fclose ($FP);

C. Read the file:
Function prototype: string fgets (int fp, int length);
Returns the result: Returns a string of length length-1, if to the end of the file, returns EOF (end of file)

Parameter description:
int FP--The file stream pointer to be read into the data, the value returned by the fopen function
int length--the number of characters to read, the actual number of characters read into length-1
Example: Reading 9 characters from a $fp
$str 1 = fgets ($fp, 10);

D. Writing files:
function prototypes: int fputs (int fp, string str, int [length]);
Return results: Same as Fclose
Parameter description:
int FP--a file stream pointer to write information, the value returned by the fopen function
String STR-strings to write to the file.
int length--The length of the write, optional, if length is not provided, the entire string is written,
Otherwise, length characters are written.
Example: Write "0000000001" to $fp
Fput ($fp, "0000000001");

B. Related String Functions:
A. Calculating the length of a string:
Function prototype: int strlen (string str);
Return Result: The length of the returned string
Parameter description:
String STR--strings to be computed for length
Example: Calculating the string length of "000000000"
$str 2 = "000000000";
$len 2 = strlen ($STR);

B. String addition: The simplest, with one. Connect the two strings together.
Example: adding $str 1 and $STR2
$str = $str 1. $str 2

C. Related graphical functions:
A. Create a new Image:
function prototypes: int imagecreate (int x_size, int y_size);
Return result: Returns an empty image identification number (ImageID) with a x*y pixel size
Parameter description: X_size,y_size is the width and height (in pixels) of the new image, respectively
Example: Create an empty image with a 88*31 pixel size
$ImageID = Imagecreate (88, 31);

B. Assign a color to the image:
function prototypes: int imagecolorallocate (int im, int red, int green, int blue);
Return result: Returns an RGB color identification number to the image ($im)
Parameter description: int im image identification number
int red, green, blue are the three colors of red, green and blue respectively, the value range 0-255
Example: Assigning an image $im an identification number of $white white color, white RGB for (255,255,255)
$white = Imagecolorallocate ($im, 255, 255, 255);
C. Fill the image with color:
function prototypes: int imagefill (int im, int x, int y, int col);
Return Result: 1 returned successfully, otherwise 0
Parameter description: int IM, image identification number
int x, int y, fills the color from the (x, y) coordinates of the image
(0,0) indicates the upper-left corner of the image
int col, color identification number
Example: Fill in black from the upper left corner of the image (that is, the entire picture) (already using the Imagecolorallocate function
A black color identification number of $black is defined.
Imagefill ($im, 0, 0, $black);

D. Calculate the width of the image:
Function prototype: int imagesx (int im);
Return result: The width of the returned image (in pixels)
Parameter description: int IM, identification number of the image.
Example: Calculating the width of an image $im
$px = Imagesx ($im);

E. Write horizontal text in the image:
function prototypes: int imagestring (int im, int font, int x, int y, string s, int col)

Return Result: 1 returned successfully, otherwise 0
Parameter description: int IM, image identification number
int font, font identification number, built-in fonts 1 to 5, users can use Imageloadfont () themselves?
?
Download fonts.
int X,int y, start writing the font's coordinates (0,0) to the upper-left corner of the picture.
string s, string to be written
int col, the color identification number of the font
Example: Write a font size of 3 in the image (3,3) position, white (already with imagecolorallocate () function
Defines a string "E&j Counter" with a black color identification number of $white)
Imagestring ($im, 3, 3, 3, "E&j Counter", $white);

F. Draw a line in the image:
function prototypes: int imageline (int im, int x1, int y1, int x2, int y2, int col);
Return Result: 1 returned successfully, otherwise 0
Parameter description: int IM, image identification number
int X1,int Y1, the starting coordinate of the dash
int X2,int Y2, the stop coordinate of the dash
int col, line color identification number
Example: $im a line from (1,14) to (85,14) a $white color in an image
Imageline ($im, 1, $white);

G. Output the image into GIF format:
Function prototype: int imagegif (int im, string filename);
Return Result: 1 returned successfully, otherwise 0
Parameter description: int IM, image identification number
string filename, the name of the generated picture, optional, if filename is empty, then direct?
Neon su?
Image, you need to pre-define the PHP output in the header ("Content-type:image/gif")
capacity as a picture
Example: Output image $im as a picture with file name "Image1.gif"
Imagegif ($im, "image1.gif");

H. Releasing the Image:
Function prototype: int Imagedestroy (int im);
Return Result: 1 returned successfully, otherwise 0
Parameter description: int IM, the image identification number to release. This function releases the image and image of the ID im
The system resources used.
Example: releasing an image $im
Imagedestroy ($im);

3. How to install this counter:
A. The system must have a PHP interpreter installed. PHP can be downloaded from http://www.php.net/, and there is a
With
Technical information can be viewed or downloaded for reading. How to install PHP please refer to its own instructions.
B. Copy the following list of programs into a file, and take the extension PHP into a directory that can run PHP scripts?
Brigade GUI?
and create a plain text file under the directory named Zzm.txt. The purpose of this file is to record the past
Visiting people
Number of uses. You can pre-set the initial value of the counter, for example 5000.
C. How do I invoke this counter on my Web page? You can call it in the following ways:



Attached: complete list of programs
Header ("Content-type:image/gif");
Defining the output as an image type

$fp = fopen ("Zzm.txt", "R");
Open a file that records previous visitors in read Zzm.txt

$str 1 = fgets ($fp, 10);
Read 9 characters from a file, this counter can record the maximum number of visitors is 999999999

$str 1++;
Counter Join

Fclose ($FP);
Close File

$fp = fopen ("Zzm.txt", "w");
Write a way to open the record number of visitors to the file Zzm.txt

Fputs ($fp, $str 1);
Write the latest number of visitors to the file

Fclose ($FP);
Close File

/*
The following is a formatted output of the number of visitors, if the number of visitors is not 9 digits, for example, 5000 (4-bit),
The number of visitors is converted to 000005000 of the form output. method is to calculate the number of digits for the number of visitors, and
Compare it to 000000000 digits (9 bits) to get the number of digits that are not in front of the number
should be a 0. For example, 5000, and 2 of the length of the difference 5, so to 5000 in front of 5 0.
*/

$len 1 = strlen ($str 1);
Number of digits to count the number of visitors

$str 2 = "000000000";
$len 2 = strlen ($STR);
Define the maximum number of digits for a counter

$dif = $len 2-$len 1;
Calculates the difference between the digits of the two, that is, the number of 0 to be mended before

$rest = substr ($str 2, 0, $DIF);
Intercept the 0 to be mended.

$string = $rest. $str 1;
Front complement 0

$font = 4;
Define font Size

$im = Imagecreate (88,31);
New images

$black = Imagecolorallocate ($im, 0,0,0);
Define Black

$white = Imagecolorallocate ($im, 255,255,255);
Define White

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

$PX = (Imagesx ($im) -8.3*strlen ($string))/2;
Calculates the horizontal coordinate at which the string begins to write, based on the length of the string, to try to make the string horizontal

Imagestring ($im, 3, $px, 2, "E&j Counter", $white);
Write "E&j Counter" to the image

Imageline ($im, 1, $white);
Unify the root horizontal line

Imagestring ($im, $font, $px, 15.5, $string, $white);
Number of written visits

Imagegif ($im);
Output images to GIF format
Imagedestroy ($im);
Release image
?>

http://www.bkjia.com/PHPjc/316134.html www.bkjia.com true http://www.bkjia.com/PHPjc/316134.html techarticle PHP has an extremely powerful image processing capability, which makes it easy to dynamically generate web images. is a simple counter made using PHP. 1. General idea: To put the past ...

  • Related Article

    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.