Generate a guest counter in PHP _php

Source: Internet
Author: User
Keywords counter visitor generate int file IM image FP
Tags truncated
Counter

Now, more and more people online, many users try to make their own homepage, the visitor counter is an essential part. Although many websites offer free counters, they are not made by themselves. Some friends may think it is difficult, dare not to try, actually have PHP this tool, it is not difficult, even can say it is easy.


First, let me talk about the idea of a visitor counter: A visitor browses to this page, and the server (such as Apache) reads the number of times that the page has been browsed from a document (for example, Num.txt, below), adds one, and then saves it back to Num.txt, and displays the number of times after the addition in the browser. If another visitor browses to this page, the server repeats the process, which implements the guest counter.


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


The function that the program needs to use now is explained:


1. Open file operation: int fopen (string filename, string mode);


where string filename is the file name to open and must be in the form of a string. For example \ "Num.txt\".


String mode is the way the file is opened and must be in the form of a character.


' R ', read-only, and the file pointer points to the beginning of the file.


' r+ ', readable and writable, with the file pointer pointing to the beginning of the file.


' W ', write-only form, the file pointer to the beginning of the file, the length of the file truncated to 0, if the file does not exist, will attempt to create a file.


' w+ ', readable and writable, the file pointer to the beginning of the file, the length of the file truncated to 0, if the file does not exist, will attempt to create a file.


' A ', append form (can only be written), the file pointer to the end of the file, if the file does not exist, will attempt to build the file.


' A + ', readable and writable, the file pointer pointing to the end of the file, if the file does not exist, will attempt to create a file.


2. Read file operation: string fgets (int fp, int length);


where int fp is the file stream pointer to be read into the data, the value is returned by the fopen function.


int length is the number of characters to be read, and the number of characters actually read is length-1.


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


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


String STR is the string to write to the file.


An int length is a write-up, optional, if length is not selected, the entire string is written. Otherwise, length characters are written.


4. Close file operation: int fclose (int fp);


where int fp is the file stream pointer returned by the fopen function.

Now let's look at the prototype of the counter: (assuming that the Num.txt file exists)



$fp = fopen (\ "num.txt\", \ "R\");


Read-only open Num.txt file


$num = Fgets ($fp, 5);


Read 4-bit numbers


$num + +;


Browse Times plus One


Fclose ($FP);


Close File


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


Write-only method to open Num.txt file


Fputs ($fp, $str 1);


Write add one after result


Fclose ($FP);


Close File


echo \ "$num \";


Browser output views


?>


What needs to be explained is that this is just a prototype of the counter, it can only be displayed in text, not beautiful, and PHP has a very powerful image processing capabilities, it can be very easy to dynamically generate web images. The above prototype has been modified to make it a really useful counter.


The idea is this: using the method in the prototype to get the number of visits, the number is converted to a standard format, image processing, and output into pictures, display.

If you want to generate a count image, you need the following function:


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


Where string str is the string to calculate the length of.


2. Add a string:


For example, add $string 1 and $string2:


$string = $string 1. $string 2


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


Where X_size,y_size is the width and height (in pixels) of the new image, respectively.


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


where int im is an image identification number.


int red, green, blue are the three colors of red, green and blue respectively, the value range 0-255, that is, the corresponding color RGB.


5. Function to fill the image with color: int Imagefill (int im, int x, int y, int col);


where int x, int y is the image coordinate at which to start the fill color, in the upper-left corner of the image (0,0).


The int col is the color identification number.


6. Functions that 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.


The int font is a font identification number.


int X,int y is the coordinate at which to start writing the font, (0,0) is the upper-left corner.


String S is the one to write.


The int col is the color identification number of the font.


7. Functions that line up 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 is the starting and ending coordinates of the dash.


The int col is the color identification number of the line.


8. function to output the image in 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 picture, optionally, if filename is empty, the output is direct.


9. Release the Image: int Imagedestroy (int im);


where int im is the image identification number to release.


This function releases the image of the ID IM and the system resources occupied by the image.


You can call this counter on your home page like this:


The following is a list of COUNTER.PHP3 's programs:



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


Defining the output as an image type


$n = 10;


Variable $n is the number of display bits


$fp = fopen (\ "num.txt\", \ "R\");


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


$str 1++;


Fclose ($FP);


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


Fputs ($fp, $str 1);


Fclose ($FP);


Same prototype


$str 2 = \ "\";


$len 1 = strlen ($str 1);


for ($i =1; $i <= $n; $i + +) {


$str 2 = \ "0\". $str 2;


};


Get $n bit 0


$len 2 = strlen ($str 2);


Number of digits to count the number of visitors


$dif = $len 2-$len 1;


$rest = substr ($str 2, 0, $DIF);


$string = $rest. $str 1;


Number of digits if not enough $n bit, in front of 0


for ($i =0; $i <= $n-1; $i + +) {


$str [$i]=substr ($string, $i, 1);


};


Store each bit in an array


$font = 4;


Define font Size


$im = Imagecreate ($n *11-1,16);


New images


$black = Imagecolorallocate ($im, 0,0,0);


$white = Imagecolorallocate ($im, 255,255,255);


Define Color


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


Set the background of the counter to black


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


for ($i =1; $i <= $n-1; $i + +) {


Imageline ($im, $i *11-1,0, $i *11-1,16, $white);


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


};


Writes each bit to the image, separated by a vertical bar


Imagegif ($im);


Image output


Imagedestroy ($im);


Release image


?>


Also, for convenience, you can use the counter as a function mycounter () so that you only need to add require ("filename") at the beginning of the home page, so that MyCounter () becomes part of this home page and, when needed, Add it to the place where you need the counter.

  • Related Article

    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.