How to generate an access counter in PHP

Source: Internet
Author: User

Use nowSome 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 generate an access counter in PHP. 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 a file: int fopen (string filename, string mode); Where 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. Read File Operation: string fgets (int fp, int length); where 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 operation: int fputs (int fp, string str, int [length]); where 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); 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: (hypothetical num.txt file exists)

 
 
  1. <? Php $ fp = fopen ("num.txt", "r ");
  2. // Open the num.txt file only.
  3. $ Num = fgets ($ fp, 5 );
  4. // Read four digits
  5. $ Num ++;
  6. // Browsing times plus one
  7. Fclose ($ fp );
  8. // Close the file
  9. $ Fp = fopen ("num.txt", "w ");
  10. // Open the num.txt file only.
  11. Fputs ($ fp, $ str1 );
  12. // Write the result after adding one
  13. Fclose ($ fp );
  14. // Close the file
  15. Echo "$ num ";
  16. // Browser outputs browsing times
  17. ?>

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 for PHP to generate an access counter 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); Where 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 indicate the width and height of the newly created image (in pixels ).

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 three colors respectively. The value ranges from 0 to 255, that is, the RGB color of the corresponding color.

5. function for filling the color of the image: int imagefill (int im, int x, int y, int col); where int x and int y are the coordinates of the image starting to fill the color, 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 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 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. The function that draws 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 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); 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 is output directly.

9. Release the image: int imagedestroy (int im); where 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. On your home page, you can call this counter to generate an access counter in PHP: The following is a list of counter. php3 programs:

 
 
  1. <?
  2. Header ("Content-type: image/gif ");
  3. // Define output as image type
  4. $ N = 10;
  5. // Variable $ n is the number of digits displayed
  6. $ Fp = fopen ("num.txt", "r ");
  7. $ Str1 = fgets ($ fp, $ n + 1 );
  8. $ Str1 ++; fclose ($ fp );
  9. $ Fp = fopen ("num.txt", "w ");
  10. Fputs ($ fp, $ str1 );
  11. Fclose ($ fp );
  12. // Same as the prototype
  13. $ Str2 = "";
  14. $ Len1 = strlen ($ str1 );
  15. For ($ I = 1; $ I <= $ n; $ I ++)
  16. {$ Str2 = "0". $ str2 ;};
  17. // Get $ n Bit 0
  18. $ Len2 = strlen ($ str2 );
  19. // Calculate the number of visits
  20. $ Dif = $ len2-$ len1;
  21. $ Rest = substr ($ str2, 0, $ dif );
  22. $ String = $ rest. $ str1;
  23. // If the number of digits is less than $ n, add 0 in front.
  24. For ($ I = 0; $ I <= $ n-1; $ I ++)
  25. {$ Str [$ I] = substr ($ string, $ I, 1 );};
  26. // Store each object in an array
  27. $ Font = 4;
  28. // Define the font size
  29. $ Im = imagecreate ($ n * 11-1, 16 );
  30. // Create an image
  31. $ Black = ImageColorAllocate ($ im, 0, 0 );
  32. $ White = ImageColorAllocate ($ im, 255,255,255 );
  33. // Define the color
  34. Imagefill ($ im, 0, 0, $ black );
  35. // Set the background color of the counter to black.
  36. ImageString ($ im, $ font, 1, 0, $ str [0], $ white );
  37. For ($ I = 1; $ I <= $ n-1; $ I ++)
  38. {Imageline ($ im, $ I * 11-, $ I * 11-, $ white); ImageString ($ im, $ font, $ I * 11 +, $ str [$ I], $ white );};
  39. // Write each image and separate it with a vertical line
  40. ImageGif ($ im );
  41. // Image output
  42. ImageDestroy ($ im );
  43. // Release the image
  44. ?>

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, when necessary, set <? MyCounter ();?> Add the counter to generate an access counter in PHP.


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.