Home arrow PHP arrow Page 3 - 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 - Showing the Statistics Table
(Page 3 of 7 )

Our next task is to code the site that will show us the saved statistics from the database. I will show two examples. The first one is text-based. Its goal is to allow you to review your visitors and visited sites. It's simple and quite obvious so you can get the data that you need from it.

This page consists of two tables. The first table shows all your visitors, each row represents a user. Data is retrieved from the database, using the MySQL GROUP BY clause. The table has the following columns:

1. Hostname
2. Number of visited pages
3. Clear data from this hostname





The picture above is my solution on my personal homepage. The 'X' signs in the last column are links referring to the code that deletes all visits from the given visitor. The code below shows the first table.

   $q='SELECT Host, COUNT(Site) AS cs FROM hits GROUP BY Host';
=mysql_query($q) or print(mysql_error());
"<h3>Visitors</h3><table width=100% border=1><tr>n".
"<td><b>Visited pages</td>n".
"<td align=center><b>Clear all from this visitor</td></tr>n";
   while (
echo "<tr><td>".$visitor['Host']."</td>n".
"<td align=center><a href=?op=stat&delhost=".
// This link is to delete the visited pages of this user
                    // Note urlencode() which is because the string
                    // that will be queried as an URL
echo "</table>";

The second table shows all the page impressions, directly from the database. Columns are:

  1. Visited page
  2. Hostname
  3. Date

page impression





Clicking on the visitor's hostname in the table above clears the data, like the previous query (the links refer to the same piece of code).

In the first column there is the title of the HTML, but it's a link using the saved URL.

And now let's see the code for the second table:

   $q='SELECT Title, Site, Host, Date FROM hits
          ORDER BY Date DESC, Host'
=mysql_query($q) or print(mysql_error());
"<h3>Page impressions</h3>".
"<table width=100% border=1><tr>n".
"<td><b>Visited page</td>n".
   while (
echo "<tr><td><a href=?".$page_imp['Site'].">".
// Link to the page
        "<td><a href=?op=stat&delhost=".$page_imp['Host'].">".
echo "</table>";

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