Sunday, April 12, 2009

Site statistics with PHP and MySQL

Last week I developed a module for my websites to keep record of visitors on any of my websites. Objective was to make such a module that can be hosted on one location and can be used in sites on different servers.

Main features of the module are;

  • Count every visit on a page.
  • Count unique visitors on the same page.
  • Show the figures on the page.


Result of all above implantation is in the picture below that shows the statistics of this post. This is a real execution of the code, it records the visitors on this page. You can see the changes in numbers by refreshing this page.



Then I built another module to use the data generated by first module to create a graphical representation for the history of last thirty days.


This is also iThis is also integrated in all my blogs, in side panel. You might can see this in action in the side panel of this page.After completing all this, now I have data that can be used in many ways. I extended with another module that can show me the summary of visits on all of my sites as below.


Another graph I created to see the share of visitor each of my site is getting.


Another graph shows the detailed comparison of each site date wise number of visitors;


Technical Details:
I have written all the code in PHP to achieve this functionality and used MySql to manage its data. For graphs I used PHPGraphLib modules.

Counter:
Initially I used a PHP graphical hit counter to count the visits on my site, that stores the data in a file on the server. But I gradually changed it completely to store data in a MySql database, by keeping its graphical digits display techniques as it is. Plus I extended the same module to count both visits and unique visitors.

Download the source code of this article from CodeProject.

No comments:

Post a Comment