All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Create A Simple PageView Counter Using PHP and MySQL

Last Updated : Jul 1, 2023

IN - PHP MySQL | Written & Updated By - Amruta

In this tutorial we will create PageViews counter system using PHP and 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.

You may also like simple value countdown using jQuery.

Create A Simple PageView Counter Using PHP and MySQL

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.

I hope this tutorial on count page views php helps you and the steps and method mentioned above are easy to follow and implement.