Home Backend Development PHP Tutorial Decimal Counter A simple counter written in php3

Decimal Counter A simple counter written in php3

Jul 29, 2016 am 08:33 AM

PHP has extremely powerful image processing capabilities, and it can easily and dynamically generate web images.
The following is a simple counter made using php.
1. General idea:
Record the number of previous visitors in a text file. When the web page is visited, open the file
and read the number of previous visitors from it, add 1 to get the latest number of visitors, and Format the number into a standard format, then call the image processing function to output the number into a picture, and then write the new visit number back to the file that records the number of visitors.
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 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", etc. O String Mode -The way to open the file must be character form.
              'r', read-only form, the file pointer points to the beginning of the file                                                                                .               out being out out out out out out out Over  to                                                                    Truncated to 0,
            If the file does not exist, an attempt will be made to create the file. '' W+', readable and written, the file pointer points to the beginning of the file, cut the file length into 0,
If the file does not exist, try to establish a 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 form
$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);
Return result: return length -1 length string, if it reaches the end of the file, return EOF (End Of File)
  Parameter description:
      int fp – the file stream pointer to read the data, the value returned by the fopen function
    int length – the number of characters read Number, 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 -- to write The string entered into the file.T int length -the length of the writing, optional, if not provides a length, the entire string will be written, otherwise, write the length of the length.
Example: Write "0000000001" to $fp
fput($fp, "0000000001");
B. Related string functions:
a. Calculate string length:
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. Adding strings: It’s the simplest, use a . to connect two strings.
Example: Add $str1 and $str2
$str = $str1.$str2
C. Related graphics functions:
a. Create a new image:
Function prototype: int imagecreate(int x_size, int y_size);
Return Result: Return an empty image identification number (ImageID) with size of 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:                                                                                                                         Image im) returns an RGB color identification number
Parameter description: int im image identification number
                                                                                                                                                                                                           ’ s ’ s ’ s ’ s ’ s         ’ ’’ ’ ’ s ’ ’ s ’ ’ s ’ ’ s 1 -- m m- rg The identification number is $white white color, and the white RGB is (255,255,255)
                                                                                                   , int x, int y, int col);
Return result: Return 1 successfully, 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: starting from the upper left corner of the image (i.e. the entire picture) fill in black (the imagecolorallocate function
  has been used to define the color identification number of black as $black).
Imagefill($im, 0, 0, $black);
d. Calculate the width of the image:
Function prototype: int imagesx(int ​​im);
Return result: Return the width of the image (unit is pixel)
Parameter description: int im, the identification number of the image.
Example: Calculate the width of 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: Return 1 if successful, otherwise return 0
Parameter description: int im, image identification number
Int font, font identification number, built-in fonts 1 to 5, users can use imageloadfont() themselves?
??? , String S, string to write
int color, color recognition number of the font
Example: In the image (3,3) position, the font size is 3, and the color is white The color identification number is $white)'s string "E&J Counter"
                                                          ‐ ‐       ‐ ‐ ‐ ‐ 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: Return 1 successfully, otherwise return 0
Parameter description: int im, the identification number of the image
Int x1, int y1, The starting coordinate of the line is int x2, int y2, the ending coordinate of the line is int col, and the color identification number of the line is
Example: Draw a line from (1,14) to (85,14) in the image $im with the color $white’s straight line
        imageline($im, 1, 14, 85, 14, $white);                                   Output the image into GIF format:                                                 ‐ out out ‐ ‐ ‐‐‐‐ �� � � �
� � Function prototype: int imagegif(int im, string filename); �
� Return result: Returns 1 successfully, otherwise returns 0
Parameter description: int im, image identification number
        String filename, generate the name of the picture, optional, if filename is empty, directly?
氖涑?
          Picture, you need to use Header ("Content-type: image/gif") Predefine the content output by php as a picture
Example: Output the image $im into a picture with the file name "image1.gif"
Imagegif($im, "image1.gif" ");
h. Release the image:
Function prototype: int imagedestroy(int im);
Return result: Return 1 if successful, otherwise return 0
Parameter description: int im, the image identification number to be released. This function will release the image of the identification number im and the system resources occupied by the image.
Example: Release image $im
Imagedestroy($im);
3. How to install this counter:
A. The system must have a PHP interpreter installed. PHP can be downloaded at http://www.php.net/. There is also a lot of detailed technical information on this site that can be browsed or downloaded for reading. Please refer to its own instructions for how to install PHP.
B. Copy the following program list into a file with the extension php, put it into a directory that can run php scripts?
Lvqiu?
And create a plain text file under the directory with the name zzm.txt .The purpose of this file is to record the number of visitors in the past. You can preset the initial value of the counter, for example 5000.
C. How to call this counter on the web page? You can call it in the following way:

Attachment: The complete program list
Header("Content-type: image/gif");
/ /Define the output as image type
  $fp = fopen("zzm.txt", "r");
  //Open the file zzm.txt that records the number of previous visitors in read form  
  $str1 = fgets($fp,10) ; 文 // Read 9 characters from the file. The maximum number of visits recorded in this counting machine is 999999999
$ STR1 ++;
// counter adding
fclose ($ fp); "zzm.txt", "w");
//Open the file zzm.txt that records the number of visitors by writing
fputs($fp, $str1);
//Write the latest number of visitors to the file
fclose ($fp);
//Close the file
/*
The following is the formatted output of the number of visitors. If the number of visitors is less than 9 digits, such as 5000 (4 digits),
Then convert the number of visitors into the form of 000005000 output. The method is to calculate the number of digits in the number of visitors, and compare it with the number of digits in 000000000 (9 digits) to get the difference in digits, and then add a 0 in front of the number. For example, the lengths of 5000 and 000000000 differ by 5, so five zeros must be added in front of 5000.​​​​The maximum number of counting digits
$dif = $len2 - $len1;
//Calculate the difference between the two digits, that is, the number of 0s to be added in front
$rest = substr($str2, 0, $dif);
//Interception 0 to be added
$string = $rest.$str1;
//Add 0 in front $font = 4;
//Define font size
$im = imagecreate(88,31);
//New image
$ black = ImageColorAllocate($im, 0,0,0);
//Define black , 0,0,$black);
//Set the background color of the counter to black
$px = (imagesx($im)-8.3*strlen($string))/2;
//Calculate the level at which the string starts to be written based on the length of the string coordinates, the purpose is to try to center the string horizontally
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 the number of visitors
ImageGif($ IM); 把 // output the image gif format
ImageDestroy ($ im);
// Release the image
? & gt;

The above introduces the decimal counter. A simple counter written in php3, including the content of the decimal counter. I hope it will be helpful to friends who are interested in PHP tutorials.


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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1254
24
Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Apr 17, 2025 am 12:06 AM

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values ​​to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

How does PHP type hinting work, including scalar types, return types, union types, and nullable types? How does PHP type hinting work, including scalar types, return types, union types, and nullable types? Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

See all articles