Website stats counter with PHP,MySQL and JavaScript

counterThis tutorial will explain how to create a simple page view counter or website views counter using PHP,MySQL and  JavaScript .In this tutorial I am using Java Script although it can be done without JavaScript.Reason for using JavaScript is that it will also count the page view if visitor visits a cache page and you can also host these files on another server to save resources.

Files:

1.config.php -To store your database configuration .

2.stats.php – To save and retrieve number of visitors.

3.visits.sql - To import structure of a table.

How to use -

1.Import the visits.sql  using PHPMyAdmin.

2.Change the  file config.php (given in step 2).

3.Upload the file config.php and stats.php to your server.

4.Replace the value of src attributed with location of your stats.php file and paste this code where  you want to show the statistics.

Here is the detail description for creating website stats counter with PHP,MySQL and JavaScript.

1).In first line of given code, an empty division is created  where stats of your website like total page view will display.I have used a division which webstats ID,if you want to change this ID then make sure to change in the stats.php too.


 

You can replace the source of Java Script file with your own.You just need to paste this code on the pages where you want to display the statistics.

2).cofig.php – This file is store configuration of your MySQL database .

 3).stats.php – This file with return a java script code which will replace the content of webstats division with website stats.

Customisation: Above script will only show the total page view of a website ,but changing few lines you can show more stats like Current Page Views ,Online Users,Today’s view etc.See how to do these things.

1).Show page view of current page -

Replace Line -15 with given code in stats.php.

 2).Show number of online users -

Replace Line -15 with given code in stats.php.

 3).Show today’s page views -

Replace Line -15 with given code in stats.php.

Demo Download Source

  • Soelay

    Oh That is a nice for web developer!

  • Guest

    Excellent! Thanks! Keep up the blogging!

  • http://www.theidiotspace.blogspot.com/ King_Hermy

    Nice, thanks, keep up the blogging.

  • hajar

    hai..i don’t understand step 1..can u xplain bit more please…

    • http://megarush.net/ Amit Singh

      An empty div tag with webstats id is placed on the place where you want to show the stats because using this id you can easily insert any new HTML codes by JavaScript (ex-document.getElementbyId(‘webstats’).innerHTML=’TEST';). div tag is not necessary if you want you can use span tag but important thing is that it should have an ID.

  • vovan

    Genius !

  • vovan

    any options with time() monipulation ? like in G_analytics ?

    stats for for today stats 7 days and stats for the last month.. will be helpful for marketing researchs

    • http://megarush.net/ Amit Singh

      Read customisation: there is already for today, for 7 days use this query
      $q=”select count(ip) as n from visits where site='”.$host.”‘ and time >
      DATE_add(curdate(), INTERVAL 7 DAY)”
      $q=”select count(ip) as n from visits where site='”.$host.”‘ and time
      BETWEEN (NOW() – INTERVAL 1 MONTH) AND last_day(NOW() – INTERVAL 1 MONTH)

      • vovan

        Amit Thank you so much your tutorials are amazing.

  • hrishi

    Hi,
    If I want to use this script on https:// page what changes I have to in the code?
    Thanks.

    • http://megarush.net/ Amit Singh

      Make this change

  • Guesto

    Hello! How can it count hits from social site like facebook ?

    • http://megarush.net/ Amit Singh

      You can use JavaScript code like this

      var ref=document.referrer;
      document.write(“”);

      and find the referrer using variable $_GET['ref'] .

  • 422

    Not sure I get it. Ok it works and thats all good, but why not increment a count rather than adding a row for each hit. Seems a awful waste of sql resources. Each time a new page hit, it generates a new row of data. As the script is primarily for logging page hits, surely a counter column would be a better option, then the sql rows would be limited by page id rather than hit.

    Just my thoughts, as I can see this as a great solution for low volume one page sites, but get a busy site with hundreds of thousands of uniques and sql would melt wouldnt it?

    • http://megarush.net/ Amit Singh

      If you don’t need counter based on time like hits on last 24 hours,last 7 days etc then you can add an extra column but if you need detail stats you have to insert a new row every time.

  • togo

    When i do refresh on page with stats it’s adding + 1 how to resolve this problem ?

    Regards.