Home > Article > Backend Development > A simple counter written in php, writing a simple counter in php_PHP tutorial
php has extremely powerful image processing capabilities, and it can be used to easily dynamically generate web images.
The following is a simple counter made using php.
1. General idea:
Record the number of past visitors in a text file. When the web page is visited, open the file
and read out the number of past visitors, and add 1. Get the latest number of visitors, format the number into the
standard format, then call the image processing function to output the number into a picture, and then write the new number of visitors back to
to record the number of visitors. in the file.
2. Function description used by the program:
A. Related file operations:
a. Open file:
Function prototype: int fopen(string filename, string mode);
Return result: If the file is opened successfully, the function returns the file stream pointer, otherwise it returns FALSE(0).
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", etc.
String mode -- The way to open the file, must be in character form.
'r', read-only form, 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 form, the file pointer points to the file at the beginning, truncate the file length to 0,
If the file does not exist, an attempt will be made to create the file.
'w+', readable and writable, the file pointer points to the beginning of the file, truncate the file length to 0,
If the file does not exist, it will try to create the file.
'a', append form (can only be written), the file pointer points to the end of the file, if the file
does not exist, an attempt will be made to create the file.
'a+', readable and writable, the file pointer points to the end of the file, if the file does not exist,
will try to create the file.
Example: Open "zzm.txt" under the current directory in read-only mode
$fp = fopen("zzm.txt", "r");
b. Close the file:
Function prototype: int fclose(int fp);
Return result: 1 is returned on success, 0 is returned on failure
Parameter description: int fp is the file stream pointer returned by the fopen function.
Example: Close the zzm.txt file just opened with fopen
fclose($fp);
c. Read the file:
Function prototype: string fgets(int fp, int length ; File stream pointer, the value returned by the fopen function
int length -- the 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 file:
Function prototype: int fputs(int fp, string str, int [length]);
Return result : Same as fclose
Parameter description:
int fp -- the file stream pointer to write information, the value returned by the fopen function
string str -- the string to be written to the file.
int length -- the length to write, optional. If length is not provided, the entire string will be written.
Otherwise, length characters will be written.
Example: Write "0000000001" to $fp
fput($fp, "0000000001");
B. Related string functions:
a. Calculate the length of the string:
Function prototype: int strlen(string str);
Return result: Return the length of the string
Parameter description:
String str -- the string whose length is to be calculated
Example: Calculate the string length of "000000000"
$str2 = "000000000";
$len2 = strlen($str);
b. Addition: Easiest However, use a . to connect the two strings.
Example: Add $str1 and $str2
$str = $str1.$str2
C. Related graphics functions:
a. New image:
Function prototype : int imagecreate(int x_size, int y_size);
Return result: Return an empty image identification number (ImageID) of X*Y pixel size
Parameter description: x_size, y_size are the width and height of the new image respectively ( In pixels)
Example: Create a new empty image of 88*31 pixel size
$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: Return an RGB color identification number to the image ($im)
Parameter description: int im image identification number Int Red, Green, and Blue are the weight of three colors of red, green and blue, respectively. )
$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: Return 1 if successful, otherwise return 0
Parameter description: int im, the identification number of the image
int x, int y, start filling the color from the (x, y) coordinates of the image
(0,0) represents the upper left corner of the image
int col, the identification number of the color
Example: Fill in black starting from the upper left corner of the image (that is, the entire image) (has been defined with the imagecolorallocate function
The color identification number for black is $black).
imagefill($im, 0, 0, $black);
d. Calculate the width of the image:
Function prototype: int imagesx(int im);
Return result: return image The width (unit is pixel)
Parameter description: int im, the identification number of the image.
Example: Calculate the width of image $im
$px = imagesx($im);
Detailed description;
Modified it according to your suggestion:
if (!empty($_GET["fuhao"]))
{
$fuhao = $_GET["fuhao"];
$ u1 = $_GET["u1"];
$u2 = $_GET["u2"];
$u3 = $_GET["u3"];
switch ($fuhao)
{
case "+":
$u3 = $u1 + $u2;
break;
/* Write your own subtraction, multiplication and division
case "-"
*/
}
?>
}
?>