Making usage statistics on a web site is one of the most enjoyable things for me on the Internet. Thanks to the technology, you can see each click of each visitor, the date of the visiting, and how many seconds the user was reading your site. I use this feature to track which of my articles was read for the longest time. In this article I will show you how to store the statistics in a MySQL database, show the web stats in an HTML table and make figures based on the stats using the GD library.
Making Usage Statistics in PHP - Charting the Results (Page 6 of 7 )
There is another interesting thing, that I've done it with $visitors array. It's keys are the hostnames of the visitors, values are the number of their visits. To draw them in our figure, I use $pixel_per_visit. That's relative to the Top 1 - the visitor whose column is the highest on the diagram.
while ($stat=mysql_fetch_array($stat_r)) $visitors[$stat['Host']]=$stat['Count_ID']; // Loading up the array... $max=current($visitors); // How many pages did the Top 1 visit? $pixel_per_visit=150/$max; // This is because we get the data // in number of visits, not in pixels
$i=0; // This is for the loop, to know // the number of the current visitor
foreach($visitors as $host => $visits) { // now $visits is number of visits imagefilledrectangle($image, 35+$i*30, 180-(visits*$pixel_per_visit), 45+$i*30, 180, $pen); imagettftext($image, 8, 0, 40+$i*30, 175-($visits*$pixel_per_visit), $pen, "\windows\fonts\arial.ttf", $host); imagettftext($image, 15, 0, 32+(($i++)*30), 200, $pen,"\windows\fonts\arial.ttf", $visits); }
When you're ready with drawing the figure you have to send the output to the browser. Here are the functions and the code:
ImageGIF (resource image) - Outputs the image in GIF format
ImageJPEG (resource image) - Outputs the image in JPEG format
ImagePNG (resource image) - Outputs the image in PNG format imagepng($image);
Note that these functions accept an optional second argument, the name of a new file to save the picture there. This can be useful if you'd like to make a picture saving option for your site. And here's the result: