A chic counter.

Source: Internet
Author: User
Tags date format array current time header php and
Counter if you are a net worm, or just a fan of network programming, you must have had a personal homepage. Or give full play to your talent, create a unique, people's eyes and ears refreshing Web page, or collect the whole world of fine material, tutorials, application software, completed your network classrooms, the World software great treasure trove of professional website. Perhaps you have not been chuihuizhili to apply to a practical counter, posted to your own website, the traffic to their own site is clear; Perhaps you have spent Dickens to write your own website counters, the arbitrary editing counters of the various functions, and constantly complete the project you want to count. But, in any case, having a cool counter is a kind of enjoyment, a kind of gratification that is not there before.
(--The above content only represents the user's personal opinion, does not represent this website's position--so-and-so website. :-D)

Now, the popular counters are nothing more than the CGI, ASP, PHP and other articles written in a statistical page is accessed by the number of server-side running programs. There are mainly embedded program code included in the program or HTML code, or programs that are introduced into the Web page. This article is about using a separate program to generate a GIF image and is included in our homepage (http://www.21php.com).

The principle of this counter is very simple, as long as you understand a little C language or other programming language, you can fully understand every line of program code; This program groups the data that a user accesses a page by 24 hours, and then saves it as an array at different times (comma, "split"), and whenever the user accesses the page, The current period of data increase by 1, save the file, and proportional output histogram; If you are interested, you can also refer to the prototype constantly develop a more useful site counters.
This counter has two main points: first, read the data of the file. Second, according to a certain algorithm will be different periods of access to a certain proportion of the image, and then output to the browser, to display.
The following is the source code of this counter and some rough explanations of the author's Bobo.

?
############################################################
#***** counters *****#
#***** writed by Boban *****#
#***** at November 23, 2000 *****#
############################################################
Submit picture File Information Header
Header ("Content-type:image/gif");
####
Macro definition, the left margin of the predefined printed text is 10 of the value of ML
You can adjust the problem according to the requirements of debugging.
Define ("ML", 10); Left margin

$IMGH = 30;
$IMGW = 300;
$Visited =array ();

If the file counter does not exist, create a file
if (!file_exists ("Counter"))
{
For ($i =0 $i <24; $i + +)
$Visited [$i]=0;
$STR = $Visited [0];
For ($i =1 $i <24; $i + +)
$str. = ", $Visited [$i]";
Format data and then write to file
$FP =fopen ("Counter", "w");
Fputs ($fp, $STR);
Fclose ($FP);
}
Else
{
read out the value of the counter
$FP =fopen ("Counter", "R");
$str =trim (fgets ($fp, 100));
$Visited =explode (",", $str);
Converts data into an array of format outputs
Fclose ($FP);
}

$Visited [Date ("G")]=intval ($Visited [Date ("G")]) +1;
1 increase in current time traffic
$max =intval ($Visited [0]);
$STR = $Visited [0];
For ($i =1 $i <24; $i + +)
{
$str. = ", $Visited [$i]";
if ($max <=intval ($Visited [$i])
$max =intval ($Visited [$i]);
}
Find the maximum number of visits
$FP =fopen ("Counter", "w");
Fputs ($fp, $STR);
Fclose ($FP);

$img =imagecreate ($IMGW +1, $imgh *2+1);
$bgcolor =imagecolorallocate ($img, 255,255,255);
$textcolor =imagecolorallocate ($img, 255,0,0);
$linecolor =imagecolorallocate ($img, 99,99,99);

Imagerectangle ($img, 0,0, $IMGW, $imgh *2, $linecolor);
$sum = 0;
For ($i =0 $i <24; $i + +)
{
Imagefilledrectangle ($img, ml+ $i *12, $IMGH-$Visited [$i]*25/$max, ml+ $i *12+4, $IMGH, $linecolor);
Imagestring ($img, 1,ml+ $i *12, $imgh +4, $i, $textcolor);
$sum + + $Visited [$i];
}
The above is proportional output histogram

Imagestring ($img, 3,10, $IMGH +15, "total: $sum", $textcolor);
Imagestring ($img, 3, $IMGW/3, $IMGH +15, "Design by 21PHP Work Studio", $textcolor);

Imagegif ($IMG);
Imagedestroy ($IMG);
Generate images, clear memory
?<

The above procedures are operating in the Php+apache environment, the use of fashion should pay attention to the following matters:
1. Permission to run the program, please note that your Web page can run PHP program.
2. File read and Write permission, because the program is running process to read and write the program. And generally, the operation of Apache is nobody, that is, only the lowest permissions, so it is best to open a new directory, its permissions set to nobody, the counter program is also placed in this directory.
The above program effect, can go to my website (http://www.21php.com) Look, if need source code, also can go to our website to download source code. The article is also very welcome to the major sites reproduced, as long as the "author Boban and transferred from Http://www.21php.com" can be.
Boban<boban@21php.com> December 1, 2000 0:18 minutes


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.