This 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 .
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?php // change these variables $host="localhost"; //host $uname="";//MySQL username $pass="";//MySQL password $db=""; //MySQL Database //don't need to change $con = mysql_connect($host,$uname,$pass); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db($db, $con); |
3).stats.php – This file with return a java script code which will replace the content of webstats division with website stats.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<?php header("content-type:text/javascript");//Tell the browser that it's a java script file. header("Cache-Control: no-cache");// prevent caching header("Pragma: nocache"); session_start(); $ses_id=session_id(); $url=$_SERVER['HTTP_REFERER']; //get referral $host=parse_url($url); $host=$host['host']; $ip = $_SERVER["HTTP_CF_CONNECTING_IP"] ? $_SERVER["HTTP_CF_CONNECTING_IP"] : $_SERVER["REMOTE_ADDR"]; //get original IP include("config.php"); //connect to database $q="insert into visits (url ,site,ip) values ('".$url."','".$host."','".$ip."')"; $r=mysql_query($q); //insert visitor log if(!$q) echo mysql_error(); $q="select count(ip) as n from visits where site='".$host."'"; //retrieve visitors log $r=mysql_query($q); while($row=mysql_fetch_array($r)){ $all=$row['n']; } $result=" Total Views.<b>".$all."</b>"; $con='document.getElementById(\'webstats\').innerHTML="Total Views:<b>'.$all.'</b>";'; echo $con; //output java script codes |
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.
|
1 |
$q="select count(ip) as n from visits where url='".$url."'"; |
2).Show number of online users -
Replace Line -15 with given code in stats.php.
|
1 |
$q="select count(ip) as n from visits where site='".$host."' and time > DATE_SUB(NOW() , INTERVAL 10 MINUTE)"; |
3).Show today’s page views -
Replace Line -15 with given code in stats.php.
|
1 |
$q="select count(ip) as n from visits where site='".$host."' and time > CURDATE()"; |
Incoming Search Terms:
- how to make website counter in java script
- mysql jquery online counter
- php mysql using counter to echo a text after an interval of time
- statistics php mysql
