Generate Guest counters with PHP

Source: Internet
Author: User
Tags format define count numeric numeric value readable strlen truncated
Counter now, more and more people surf the internet, many netizens try to make their own homepage, the visitor counter is an essential part. Although many sites provide free counters, but after all, not their own hands to make? Some friends may think it is difficult, do not dare to try, in fact, with the tool of PHP, it is not difficult, even can say it is easy.


First of all, let me talk about the visitor counter's thinking: A visitor browses this page, and the server (such as Apache) reads the number of times the page has been browsed from a document (Num.txt, for example), adds one, and then saves it back to Num.txt and displays the number of times it was added in the browser. If another visitor browses to this page, the server repeats the above process and realizes 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 to be used by the program is described:


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


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


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


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


' r+ ', readable and writable, and 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 truncated to 0, if the file does not exist, will attempt to establish the file.


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


' A ', the Append form (writable only), the file pointer points to the end of the file, and if the file does not exist, it will attempt to establish the file.


' A + ', readable writable, file pointer to the end of the file, if the file does not exist, will attempt to establish the file.


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


where the int FP is the file stream pointer to be read into the data, the Fopen function returns the numeric value.


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


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


where the int FP is the file stream pointer to write information, the fopen function returns a numeric value.


String STR is the strings to be written to the file.


int length is the length of the write, optionally, if length is not selected, the entire string is written. Otherwise, the length character is written.


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


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

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














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


Open Num.txt File read-only mode


$num = Fgets ($fp, 5);


Read 4 digits


$num + +;


Browse Times plus One


Fclose ($FP);


Close File


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


Open Num.txt file in write-only mode


Fputs ($fp, $str 1);


Write plus one result


Fclose ($FP);


Close File


echo "$num";


Browser output Browse times


?>






It needs to be explained that this is only the prototype of the counter, it can only be displayed in the text of the number of times, is not beautiful, and PHP has a very powerful image processing capabilities, it can easily dynamically generate web images. The above prototype is modified to make it a truly practical counter.


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

If you want 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 $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.


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


where int im is an image recognition number.


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


5. function to fill the color of the image: int imagefill (int im, int x, int y, int col);


where int x, int y is the image coordinate of the starting fill color, with the upper-left corner of the image (0,0).


int col is the identification number of the color.


6. function to write horizontal text in 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 a font recognition number.


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


String s are the strings to be written.


int col is the color identification number of the font.


7. Function of line in 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.


int col is the color identification number of the line.


8. The image output into GIF format function: 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, direct output.


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


where int im is the image recognition number to be released.


This function frees the system resources occupied by the image and image of the ID im.


You can call this counter on your own home page:


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 displayed digits


$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);


Count the number of digits visited


$dif = $len 2-$len 1;


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


$string = $rest. $str 1;


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


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


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


};


Storing each bit as an array


$font = 4;


Define font Size


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


New Image


$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 and separates it with a vertical bar


Imagegif ($im);


Image output


Imagedestroy ($im);


Releasing the image


?>


In addition, in order to facilitate, you can also use the counter as a function mycounter (), so that only at the beginning of the home page to add require ("filename"), so that MyCounter () as part of this home page, when needed, will be added to the counter in place on it.



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.