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.

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.

My Personal Web Site

Recently I decided to build a web site for m y personal stuff. Initially I had two goals in my mind. To organize and publish my contents on internet and to exercise some techniques on the same web site in development.I started with a traditional website lay out in simple HTML and CSS with static contents. Then turned it into a PHP website to handle multiple pages with same layout. Then I added some coding to make it little dynamic with expending and collapsing menus with PHP and AJAX the internal processing of my PHP coding was inspired by Mambo and Joomla UI for content management but it was completely my own written code different then any other CMS. Then started adding links to my contents on flickr and you tube etc. and then I came to know a whole new world of widgets and gadget to embed and share the web contents. I did by embedding different objects from different sources, like you tube, slide share and flickr. Here is the is version of my website after implementing all above click the image below to visit the site.

When I first saw iGoogle in 2006, I was inspired by the technology and it was my first motive to know about AJAX technology. I had in my mind to make my site something like that having dynamic and moveable contents. In last six months I did lot of research on this technique red many articles and work of some other people. Finally I started building my own dashboard for my web site. I used JQuery to achieve to achieve dynamic behavior. Plus I created a standard for widgets for my dashboard after observing modules in PageFlakes and IGoogle and made a widget library for my dashboard. It is all in action now. In the site, each window is an independent widget that can be minimized, expanded and rearranged by viewer. Each widget has its own content and functionality. In this way I can add lot of contents on a single page and viewer don’t have to leave a page to go to new page. Some widgets can even be maximized to full screen or can show themselves in larger floating window on the same page.

You can visit the live website by clicking on the image below;

I will discuss some techniques I have applied in my site in my future blogs.