Generate a visitor counter using PHP

Source: Internet
Author: User
Nowadays, there are more and more people accessing the internet. many netizens try to make their own home pages. visitor counters are an essential part. Although many websites provide free counters, after all, they are not produced by themselves? Some friends may think it is difficult and dare not try it. In fact, with the PHP tool, it is not difficult, or even easy to say. First, let me talk about the access counter.

Nowadays, there are more and more people accessing the internet. many netizens try to make their own home pages. visitor counters are an essential part. Although many websites provide free counters, after all, they are not produced by themselves? Some friends may think it is difficult and dare not try it. In fact, with the PHP tool, it is not difficult, or even easy to say.


First, let's talk about the concept of visitor counters: refer, and display the number of times after adding one in the browser. If another visitor browses this page, the server repeats the above process to implement the visitor counter.


PHP does not have direct counter functions, but with its powerful functions, we can easily write a counter by ourselves.


The functions required by the program are described as follows:


1. open the file: int fopen (string filename, string mode );


Here, string filename is the name of the file to be opened, which must be in the string format. For example, \ "num.txt \".


String mode is used to open a file. it must be a character.


'R' in read-only mode. 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. the file pointer points to the beginning of the file, and the file length is cut to 0. if the file does not exist, the file will be created.


'W + ': readable and writable. the file pointer points to the beginning of the file and cut the file length to 0. if the file does not exist, create a file.


'A', in the append format (only writable). The file pointer points to the end of the file. if the file does not exist, the file will be created.


'A + ', readable and writable. the object pointer points to the end of the object. if the object does not exist, the object will be created.


2. file read operations: string fgets (int fp, int length );


Here, int fp is the file stream pointer to read data, which is returned by the fopen function.


Int length is the number of characters to be read, and the actual number of characters to be read is length-1.


3. file write operations: int fputs (int fp, string str, int [length]);


The int fp is the file stream pointer to write information, which is returned by the fopen function.


String str is the string of the file to be written.


Int length is the write length. optional. if length is not selected, the entire string is written. Otherwise, write length characters.


4. disable the file operation: int fclose (int fp );


Int fp is the file stream pointer returned by the fopen function.

Next, let's take a look at the counter prototype: (the hypothetical num.txt file exists)



$ Fp = fopen (\ "num.txt \", \ "r \");


// Open the num.txt file only.


$ Num = fgets ($ fp, 5 );


// Read four digits


$ Num ++;


// Browsing times plus one


Fclose ($ fp );


// Close the file


$ Fp = fopen (\ "num.txt \", \ "w \");


// Open the num.txt file only.


Fputs ($ fp, $ str1 );


// Write the result after adding one


Fclose ($ fp );


// Close the file


Echo \ "$ num \";


// Browser outputs browsing times


?>


It must be noted that this is only the prototype of the counter. it can only display the number of times in text mode, but it is not beautiful. PHP has extremely powerful image processing capabilities, it can be used to dynamically generate WEB images easily. Modify the prototype to make it a practical counter.


The idea is as follows: use the method in the prototype to obtain the number of visits, convert the number into a standard format, perform image processing, and output it into an image for display.

To generate a count image, you need the following functions:


1. string length function: int strlen (string str );


Here, string str is the string to calculate the length.


2. string addition:


For example, add $ string1 and $ string2:


$ String = $ string1. $ string2


3. create an image function: int imagecreate (int x_size, int y_size );


X_size and y_size are the width and height of the newly created image (in pixels ).


4. color function: int imagecolorallocate (int im, int red, int green, int blue );


Int im is the image identification number.


Int red, green, and blue are the three colors respectively. The value ranges from 0 to 255, that is, the RGB color of the corresponding color.


5. function for filling the image color: int imagefill (int im, int x, int y, int col );


Int x and int y are the coordinates of the image that begins to fill the color, and the upper left corner of the image is (0, 0 ).


Int col is the identification number of the color.


6. function for writing horizontal text into the image: int imagestring (int im, int font, int x, int y, string s, int col );


Int im is the identification number of the image.


Int font is the font identification number.


Int x, int y is the coordinate of the starting to write 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. functions for straight line in the image: int imageline (int im, int x1, int y1, int x2, int y2, int col );


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 dash.


Int col is the color identification number of a line.


8. function for outputting an image into GIF format: int imagegif (int im, string filename );


Int im is the identification number of the image.


String filename is the name of the generated image. optional. if filename is empty, it is output directly.


9. release the image: int imagedestroy (int im );


Int im is the identification number of the image to be released.


This function releases the system resources occupied by the image and image of the identification number im.


You can call this counter on your home page as follows:


The following is a list of counter. php3 programs:



Header (\ "Content-type: image/gif \");


// Define output as image type


$ N = 10;


// Variable $ n is the number of digits displayed


$ Fp = fopen (\ "num.txt \", \ "r \");


$ Str1 = fgets ($ fp, $ n + 1 );


$ Str1 ++;


Fclose ($ fp );


$ Fp = fopen (\ "num.txt \", \ "w \");


Fputs ($ fp, $ str1 );


Fclose ($ fp );


// Same as the prototype


$ Str2 = \"\";


$ Len1 = strlen ($ str1 );


For ($ I = 1; $ I <= $ n; $ I ++ ){


$ Str2 = \ "0 \". $ str2;


};


// Get $ n bit 0


$ Len2 = strlen ($ str2 );


// Calculate the number of visits


$ Dif = $ len2-$ len1;


$ Rest = substr ($ str2, 0, $ dif );


$ String = $ rest. $ str1;


// If the number of digits is less than $ n, add 0 in front.


For ($ I = 0; $ I <= $ n-1; $ I ++ ){


$ Str [$ I] = substr ($ string, $ I, 1 );


};


// Store each object in an array


$ Font = 4;


// Define the font size


$ Im = imagecreate ($ n * 11-1, 16 );


// Create an image


$ Black = ImageColorAllocate ($ im, 0, 0 );


$ White = ImageColorAllocate ($ im, 255,255,255 );


// Define the color


Imagefill ($ im, 0, 0, $ black );


// Set the background color of the counter to black.


ImageString ($ im, $ font, 1, 0, $ str [0], $ white );


For ($ I = 1; $ I <= $ n-1; $ I ++ ){


Imageline ($ im, $ I * 11-, $ I * 11-, $ white );


ImageString ($ im, $ font, $ I * 11 + 1, 0, $ str [$ I], $ white );


};


// Write each image and separate it with a vertical line


ImageGif ($ im );


// Image output


ImageDestroy ($ im );


// Release the image


?>


In addition, for convenience, you can also use the counter as a function MyCounter (), so you only need to add require ("filename") at the beginning of the home page; make MyCounter () as part of this homepage Add it to the location where the counter is needed.

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.