Join TalkersCode Now !

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

Create A Simple PageView Counter Using PHP and MySQL.

Tuesday, Apr 4, 2017

Tags:- PHP CSS MySQL


Counting or Storing your website stats is a very important for every website owner it provides you the information of hows your website doing,how many visitor visiting your website etc.In this tutorial we will create PageViews counter system using PHP and MySQL.You may also like simple value countdown using jQuery.


See Demo
PageView


To create a PageView Counter System we have to Make a PHP file and define codes for PageView Counter System and Display Total Page Views.



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

We have to get User IP Address to make PageView Counter System so that our PageView counter increments the total visit with the visit of every unique user and every unique user means every unique IP Address.You may also like date time countdown timer using JavaScript.



We have to make two database table One is for storing the webpage name and IP Address and second one is for updating the total visits every time a unique visitor visit our respective webpage

// pageview

CREATE TABLE `pageview` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `page` text NOT NULL,
 `userip` text NOT NULL,
 PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=latin1


// totalview

CREATE TABLE `totalview` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `page` text NOT NULL,
 `totalvisit` text NOT NULL,
 PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=latin1

<?php
  $host="localhost";
  $username="root";
  $password="";
  $databasename="sample";

  $connect=mysql_connect($host,$username,$password);
  $db=mysql_select_db($databasename);

  // gets the user IP Address
  $user_ip=$_SERVER['REMOTE_ADDR'];

  $check_ip = mysql_query("select userip from pageview where page='yourpage' and userip='$user_ip'");
  if(mysql_num_rows($check_ip)>=1)
  {
	
  }
  else
  {
    $insertview = mysql_query("insert into pageview values('','yourpage','$user_ip')");
	$updateview = mysql_query("update totalview set totalvisit = totalvisit+1 where page='yourpage' ");
  }
?>

<html>
<head>
</head>

<body>
  <?php
    $stmt = mysql_query("select totalvisit from totalview where page='yourpage' ");
  ?>

  <p>This page is viewed <?php echo mysql_num_rows($stmt);?> times.</p>

</body>
</html>

We dont want our counter system to increment our total visits if a same user keep visiting same page again and agian that is why we does not store the IP when a user already visit that page.Always validate data before and after submitting the form to avoid most sql injections.


Thats all, this is how to create a PageView Counter Using PHP 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