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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
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)

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

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

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

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.

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

See all articles