Join TalkersCode Now !

Get Latest Tutorials And Links On Web Development
And Get Existing Offers Via Email

Get Website Statistics Using PHP, jQuery And MySQL

Friday, Jan 27, 2017

Tags:- PHP jQuery MySQL


Website statistics like total visitors, total visitors online, total pageviews etc are very beneficial data every website owner must have to get a record of all of these to know how your site performing, how many visitor are online, how many visitor your website get per day or month.Generally website owner use external libraries or other web statistics website to get record of website and some use there own method to get website statistics if you also want to get website statistics by using your own method then this tutorial will definitely helpful for you. In this tutorial we will show you how to get website statistics using PHP, jQuery and MySQL.You may also like get visitor details using PHP.


See Demo
Get Website Statistics Using PHP, jQuery And MySQL


To Get Website Statistics It Takes Only Two Steps:-

  1. Make a PHP file and define markup and scripting
  2. Make a CSS file and define styling


Step 1.Make a PHP file and define markup and scripting

We make a PHP file and save it with a name statistics.php

// Database Structure 
CREATE TABLE 'total_visitors' (
 'id' int NOT NULL,
 'session' text NOT NULL,
 'time' int NOT NULL,
) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=latin1

CREATE TABLE 'pageviews' (
 'id' int NOT NULL,
 'page' text NOT NULL,
 'ip' text NOT NULL,
) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=latin1

CREATE TABLE 'articles' (
 'id' int NOT NULL,
 'title' text NOT NULL,
 'link' int NOT NULL,
) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=latin1

<?php
session_start();
$_SESSION['session']=session_id();

$host="localhost";
$username="root";
$password="";
$databasename="sample";
$connect=mysql_connect($host,$username,$password);
$db=mysql_select_db($databasename);

function total_online()
{
 $current_time=time();
 $timeout = $current_time - (60);

 $session_exist = mysql_query("SELECT session FROM total_visitors WHERE session='".$_SESSION['session']."'");
 $session_check = mysql_num_rows($session_exist);

 if($session_check==0 && $_SESSION['session']!="")
 {
  mysql_query("INSERT INTO total_visitors values ('','".$_SESSION['session']."','".$current_time."')");
 }
 else
 {
  $sql = mysql_query("UPDATE total_visitors SET time='".time()."' WHERE session='".$_SESSION['session']."'");
 }

 $select_total = mysql_query("SELECT * FROM total_visitors WHERE time>= '$timeout'");
 $total_online_visitors = mysql_num_rows($select_total);
 return $total_online_visitors;
}

if(isset($_POST['get_online_visitor']))
{
 $total_online=total_online();
 echo $total_online;
 exit();
}
?>

<html>
<head>
<link href="statistics_style.css" type="text/css" rel="stylesheet"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
setInterval(function()
{ 
 $.ajax
 ({
 type:'post',
 url:'',
 data:{
  get_online_visitor:"online_visitor",
 },
 success:function(response) {
 if(response!="")
 {
  $("#online_visior_val").html(response);
 }
 }
 });
}, 10000)
}); 
</script>
</head>
<body>
<div id="wrapper">

<?php

// To Get Total Online Visitors
$total_online_visitors=total_online();

// To Get Total Visitors
$total_visitors = mysql_query("SELECT * FROM total_visitors");
$total_visitors = mysql_num_rows($total_visitors);

// To Insert Page View And Select Total Pageview
$user_ip=$_SERVER['REMOTE_ADDR'];
$page=$_SERVER['PHP_SELF'];
mysql_query("insert into pageviews values('','$page','$user_ip')");
$pageviews = mysql_query("SELECT * FROM pageviews");
$total_pageviews = mysql_num_rows($pageviews);

//To Get Total Articles
$articles = mysql_query("SELECT * FROM articles");
$total_articles = mysql_num_rows($articles);
?>

<div id="stat_div">
<li><p>Total Visitors</p><br><span><?php echo $total_visitors;?></span></li>
<li><p>Visitors Online</p><br><span id="online_visior_val"><?php echo $total_online_visitors;?></span></li>
<li><p>Total Pageviews</p><br><span><?php echo $total_pageviews;?></span></li>
<li><p>Total Articles</p><br><span><?php echo $total_articles;?></span></li>
</div>

</div>
</body>
</html>

In this step we create three database tables 'total_visitors', 'pageviews' and 'articles'. We get total visitors and total online visitors from first database, we get total pageviews from second database and we get total articles published in website from third database by entering all the required data of our articles in articles table. You may also like get page loading time using PHP.

To get total online visitors we create function 'total_online' under this function we get the current time and subtract 60 from current time because we want to get accurate online visitors the smaller the time more accurate will be the value then we use session_id() function to create a session for user and insert in session variable for further use.

Now we query the database and check if this session id is present in our database and if the session id is not present in database it means the user is just open our webpage then we insert the user id with current time in our 'total_visitors' database table, if session id is present in our database it means user is already online then we update the time to current time.

Now to get all the online visitors we use very simple logic we select all the sessions who have difference between current time and inserted time less than 60 seconds to get most accurate online visitors.We use this logic because if the time is more than 60 seconds it means the user is most probably leave the website.

We use ajax function to call this function and check for online visitor in every 10 sec and all the time this function update the session time to current time so if the difference is more than 60 sec then it means user is not online.You may also like block user from website using PHP



Step 2.Make a CSS file and define styling

We make a CSS file and save it with a name statistics_style.css

body
{
 margin:0 auto;
 padding:0px;
 text-align:center;
 width:100%;
 font-family: "Myriad Pro","Helvetica Neue",Helvetica,Arial,Sans-Serif;
 background-color:#A9BCF5;
}
#wrapper
{
 margin:0 auto;
 padding:0px;
 text-align:center;
 width:995px;
}
#stat_div li
{
 list-style-type:none;
 display:inline-block;
 color:#084B8A;
 margin:20px;
 font-weight:bold;
}
#stat_div li p
{
 font-size:30px;
}
#stat_div li span
{
 padding:40px;
 background-color:#084B8A;
 color:white;
 border-radius:50%;
 font-size:30px;
}

That's all, this is how to get website statistics using PHP, jQuery and MySQL.You can customize this code further as per your requirement. And please feel free to give comments on this tutorial.

Join Us With Our 21000+ Subscribers And Get Our Latest Tutorials Update Via Email

Are You Starting A New Blog
Do You Want Best Web Hosting?

Try World's Best Web Hosting Provider BlueHost
At Just $3.95/mo

Hurry Up! Limited Time Offer

Yes, I Want To Check Out BlueHost

Check The World's Best SEO And All-In-One Marketing Toolkit
For Digital Marketing Professionals SEMRUSH

Check Out SEMRUSH