Home Backend Development PHP Tutorial The main implementation method of generating access counters in PHP_PHP Tutorial

The main implementation method of generating access counters in PHP_PHP Tutorial

Jul 15, 2016 pm 01:31 PM
php main go it accomplish try method friend now generate of counter access

Now use itSome friends may think it is difficult and dare not try it. In fact, with PHP as a tool, it is not difficult, you can even say it is easy. First, let me talk about the idea of ​​a visitor counter: a visitor browses this page, and the server (such as Apache) reads the number of times the page has been viewed from a document (num.txt is used as an example below), and adds 1, then save it back to num.txt, and display the number of times plus one in the browser.

If another visitor browses this page, the server repeats the above process, thus realizing PHP to generate an access counter. PHP does not have a direct counter function, but using its powerful functions, we can easily write a counter ourselves.

Now we will explain the functions that the program needs to use:

1. Open file operation: int fopen(string filename, string mode); where string filename is the name of the file to be opened and must be in string form. For example "num.txt". String mode is the way to open the file, which 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 beginning of the file, the file length is truncated 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, and the file length is cut to 0. If the file does not exist, an attempt will be made 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, an attempt will be made to create the file.

2. File reading operation: string fgets(int fp, int length); where int fp is the file stream pointer to read data, and the fopen function returns the value. int length is the number of characters to be read, and the actual number of characters read is length-1.

3. File writing operation: int fputs(int fp, string str, int [length]); where int fp is the file stream pointer to which information is to be written, and the value is returned by the fopen function. string str is the string to be written to the file. int length is the length to be written, optional. If length is not selected, the entire string will be written. Otherwise, write length characters.

4. Close file operation: int fclose(int fp); where int fp is the file stream pointer returned by the fopen function. Next, let’s take a look at the prototype of the access counter generated by PHP: (assuming the num.txt file exists)

<ol class="dp-xml"><li class="alt"><span><span class="tag"><</span><span> ?php $</span><span class="attribute">fp</span><span> = </span><span class="attribute-value">fopen</span><span>("num.txt", "r");   </span></span></li><li><span>//只读方式打开num.txt文件   </span></li><li class="alt"><span>$</span><span class="attribute">num</span><span> = </span><span class="attribute-value">fgets</span><span>($fp,5);   </span></li><li><span>//读取4位数字   </span></li><li class="alt"><span>$num++;   </span></li><li><span>//浏览次数加一   </span></li><li class="alt"><span>fclose($fp);   </span></li><li><span>//关闭文件   </span></li><li class="alt"><span>$</span><span class="attribute">fp</span><span> = </span><span class="attribute-value">fopen</span><span>("num.txt", "w");   </span></li><li><span>//只写方式打开num.txt文件   </span></li><li class="alt"><span>fputs($fp, $str1);   </span></li><li><span>//写入加一后结果   </span></li><li class="alt"><span>fclose($fp);   </span></li><li><span>//关闭文件   </span></li><li class="alt"><span>echo "$num";   </span></li><li><span>//浏览器输出浏览次数  </span></li><li class="alt"><span class="tag">?></span><span> </span></span></li></ol>
Copy after login

It should be noted that this is only the prototype of the counter, it can only Displaying the number of times in text is not beautiful, but PHP has extremely powerful image processing capabilities, and it can easily and dynamically generate WEB images.

The above prototype will be modified below to make it a truly practical counter. The idea behind generating an access counter in PHP is as follows: use the method in the prototype to get the number of accesses, convert the number into a standard format, perform image processing, and output it into a picture for display. If you want to generate a counting image, you need the following functions:

1. String length function: int strlen(string str); where string str is the string whose length is to be calculated.

2. Add strings: For example, add $string1 and $string2: $string = $string1.$string2

3. Create a new image function: int imagecreate(int x_size, int y_size); where x_size and y_size are the width and height of the new image (in pixels) respectively.

4. Color function: int imagecolorallocate(int im, int red, int green, int blue); where int im is the image identification number. int red, green, and blue are the components of red, green, and blue colors respectively, with a value range of 0 - 255, that is, the RGB of the corresponding color.

5. Function to fill the image with color: int imagefill(int im, int x, int y, int col); where int x, int y are the image coordinates where color filling starts, starting from the upper left corner of the image is (0, 0). int col is the identification number of the color.

6. Function to write horizontal text in an image: int imagestring(int im, int font, int x, int y, string s, int col); where int im is the identification number of the image. int font is the font identification number. int x, int y are the coordinates to start writing the font, (0,0) is the upper left corner. string s is the string to be written. int col is the color identification number of the font.

7. The function to draw a straight line in the image: int imageline(int im, int x1, int y1, int x2, int y2, int col); where int im is the identification number of the image. int x1, int y1, int x2, int y2 are the starting and ending coordinates of the drawn line. int col is the color identification number of the line.

8. Function to output image into GIF format: int imagegif(int im, string filename); where int im is the identification number of the image. String filename is the name of the generated image, optional. If filename is empty, it will be output directly.

9. Release the image: int imagedestroy(int im); where int im is the image identification number to be released. This function releases the image of the identification number im and the system resources occupied by the image. You can call this counter on your homepage like this to implement PHP to generate an access counter: The following is the program list of counter.php3:

<ol class="dp-xml"><li class="alt"><span><span class="tag"><</span><span> ?   </span></span></li><li><span>Header("Content-type: image/gif");   </span></li><li class="alt"><span>//定义输出为图像类型   </span></li><li><span>$</span><span class="attribute">n</span><span>=</span><span class="attribute-value">10</span><span>;   </span></li><li class="alt"><span>//变量$n是显示位数   </span></li><li><span>$</span><span class="attribute">fp</span><span> = </span><span class="attribute-value">fopen</span><span>("num.txt", "r");   </span></li><li class="alt"><span>$</span><span class="attribute">str1</span><span> = </span><span class="attribute-value">fgets</span><span>($fp,$n+1);   </span></li><li><span>$str1++; fclose($fp);   </span></li><li class="alt"><span>$</span><span class="attribute">fp</span><span> = </span><span class="attribute-value">fopen</span><span>("num.txt", "w");   </span></li><li><span>fputs($fp, $str1);   </span></li><li class="alt"><span>fclose($fp);   </span></li><li><span>//同原型   </span></li><li class="alt"><span>$</span><span class="attribute">str2</span><span> = "";   </span></li><li><span>$</span><span class="attribute">len1</span><span> = </span><span class="attribute-value">strlen</span><span>($str1);   </span></li><li class="alt"><span>for ($</span><span class="attribute">i</span><span>=</span><span class="attribute-value">1</span><span>;$i</span><span class="tag"><</span><span>=$n;$i++)   </span></li><li><span>{ $</span><span class="attribute">str2</span><span> = "0".$str2; };   </span></li><li class="alt"><span>//得到$n位0   </span></li><li><span>$</span><span class="attribute">len2</span><span> = </span><span class="attribute-value">strlen</span><span>($str2);   </span></li><li class="alt"><span>//计算访问人数的位数   </span></li><li><span>$</span><span class="attribute">dif</span><span> = $len2 - $len1;   </span></li><li class="alt"><span>$</span><span class="attribute">rest</span><span> = </span><span class="attribute-value">substr</span><span>($str2, 0, $dif);   </span></li><li><span>$</span><span class="attribute">string</span><span> = $rest.$str1;   </span></li><li class="alt"><span>//位数如果不够$n位,在前面补0   </span></li><li><span>for ($</span><span class="attribute">i</span><span>=</span><span class="attribute-value">0</span><span>;$i</span><span class="tag"><</span><span>=$n-1;$i++)   </span></li><li class="alt"><span>{ $str[$i]=substr($string,$i,1); };   </span></li><li><span>//以数组存储每位  </span></li><li class="alt"><span> $</span><span class="attribute">font</span><span> = </span><span class="attribute-value">4</span><span>;  </span></li><li><span> //定义字号  </span></li><li class="alt"><span> $</span><span class="attribute">im</span><span> = </span><span class="attribute-value">imagecreate</span><span>($n*11-1,16);   </span></li><li><span>//新建图象  </span></li><li class="alt"><span> $</span><span class="attribute">black</span><span> = </span><span class="attribute-value">ImageColorAllocate</span><span>($im, 0,0,0);   </span></li><li><span>$</span><span class="attribute">white</span><span> = </span><span class="attribute-value">ImageColorAllocate</span><span>($im, 255,255,255);   </span></li><li class="alt"><span>//定义颜色   </span></li><li><span>imagefill($im, 0,0,$black);   </span></li><li class="alt"><span>//把计数器的底色设置成黑色   </span></li><li><span>ImageString($im,$font,1,0,$str[0],$white);  </span></li><li class="alt"><span> for ($</span><span class="attribute">i</span><span>=</span><span class="attribute-value">1</span><span>;$i</span><span class="tag"><</span><span>=$n-1;$i++)   </span></li><li><span>{ imageline($im, $i*11-1,0,$i*11-1,16, $white); ImageString($im,$font,$i*11+1,0,$str[$i],$white); };   </span></li><li class="alt"><span>//将每位写入图象,并以竖线分隔   </span></li><li><span>ImageGif($im);  </span></li><li class="alt"><span> //图象输出   </span></li><li><span>ImageDestroy($im);   </span></li><li class="alt"><span>//释放图象   </span></li><li><span class="tag">?></span><span>  </span></span></li></ol>
Copy after login

另外,为了方便,还可以用将计数器作为一个函数MyCounter(),这样只许需在主页开头加入require(“filename”);使MyCounter()成为此主页的一部分,需要的时候,将加在需要计数器的地方就可以完成PHP生成访问计数器。


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/446219.htmlTechArticle现在用 有的朋友可能认为它很难,不敢去尝试,其实有了PHP这个工具,它并不难,甚至可以说它很容易。 首先,让我来谈一谈访客计数器...
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 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)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

See all articles