Home arrow PHP arrow Page 6 - Making Usage Statistics in PHP

Making Usage Statistics in PHP

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. 

Author Info:
By: Adam Szanto
Rating: 4 stars4 stars4 stars4 stars4 stars / 145
February 16, 2004
  1. · Making Usage Statistics in PHP
  2. · Save the HTML Title
  3. · Showing the Statistics Table
  4. · Showing Figures Based on the Statistics
  5. · Creating the Image Library
  6. · Charting the Results
  7. · Before Saying Goodbye

print this article

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.

// The vertical axe
imageline($image, 15, 25, 20, 20, $pen);
// The arrow
imageline($image, 25, 25, 20, 20, $pen);
imageline($image, 20, 180, 380, 180, $pen);
// The horizontal axe
imageline($image, 375, 175, 380, 180, $pen);
imageline($image, 375, 185, 380, 180, $pen);
while ($stat=mysql_fetch_array($stat_r))
// Loading up the array...
       // How many pages did the Top 1 visit?
       // This is because we get the data
       // in number of visits, not in pixels
// 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:

  1. ImageGIF (resource image) - Outputs the image in GIF format
  2. ImageJPEG (resource image) - Outputs the image in JPEG format
  3. 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:








blog comments powered by Disqus

- Removing Singletons in PHP
- Singletons in PHP
- Implement Facebook Javascript SDK with PHP
- Making Usage Statistics in PHP
- Installing PHP under Windows: Further Config...
- File Version Management in PHP
- Statistical View of Data in a Clustered Bar ...
- Creating a Multi-File Upload Script in PHP
- Executing Microsoft SQL Server Stored Proced...
- Code 10x More Efficiently Using Data Access ...
- A Few Tips for Speeding Up PHP Code
- The Modular Web Page
- Quick E-Commerce with PHP and PayPal
- Regression Testing With JMeter
- Building an Iterator with PHP

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials