How to Create Visitor Counters with PHP and Mysql


This time, I will be explaining how to count visitors to a website using PHP and MYSQL. Actually a lot of visitor counters that can be used for free, but it would not hurt us to try to make it yourself.

to create a visitor counter is actually not too complicated, you can easily create a visitor counter.

Please follow the steps to create a website visitor counters following:

1. Create a database on your PHPmyAdmin. and then create a table with a Anma visitor

please use the script below :

CREATE TABLE IF NOT EXISTS `visitor` (
`id` int(11) NOT NULL,
  `date` date NOT NULL,
  `ip_address` text NOT NULL,
  `counter` text NOT NULL,
  `browser` text NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

2. After creating a database, we will create a config file to connect the database. Please use the following script, use the name of the connection.php

<?php
$connection = mysql_pconnect("localhost","root","") or die ("connection Failed");
if ($
connection){
mysql_select_db("test") or die("Can't Select Database");
   }
?>
 adjust the above configuration with your data.

3. Let us all make its visitor counters. with the following script, you can calculate the existing visitor. write the following script and save it with the file name count.php

 <?php
include "connection.php";
// check the browser
$browser = $_SERVER['HTTP_USER_AGENT'];
$chrome = '/Chrome/';
$firefox = '/Firefox/';
$ie = '/MSIE/';
if (preg_match($chrome, $browser))
    $data = "Chrome/Opera";
if (preg_match($firefox, $browser))
    $data = "Firefox";
if (preg_match($ie, $browser))
    $data = "IE";

$ipaddress = $_SERVER['REMOTE_ADDR']."";
$browser = $data;
$date = date('Y-m-d');
$visitor = 1;
// Register Into Session then save to database
if (!isset($_SESSION['visitor'])){
$_SESSION['visitor']=$ipaddress;
mysql_query("INSERT INTO visitor (date,ip_address,counter,browser) VALUES ('".$date."','".$ipaddress."','".$visitor."','".$browser."')");
}
// Calculate Visitor
$yesterday  = date("Y-m-d",mktime(0,0,0,date('m'),date('d')-1,date('Y')));
$today  = mysql_fetch_array(mysql_query('SELECT sum(counter) AS today FROM visitor WHERE date="'.date("Y-m-d").'"'));
$yesterday = mysql_fetch_array(mysql_query('SELECT sum(counter) AS yesterday FROM visitor WHERE date="'.$yesterday.'"'));
$sql = mysql_fetch_array(mysql_query('SELECT sum(counter) as total FROM visitor'));
?>
 4. After successfully creating a page that contains a data storage visitors, it's time we make a page to display the data of visitors. create a file with the name appearing-visitor.php
copy the following code. :

<html>
<head>
 <title>Visitor Counter</title>
</head>
<body>
<?php
 session_start();
 include "count.php";
 echo "<div style='margin-top:50px; font-family: Segoe UI; font-weight: lighter;'><center>";
 echo "<b>---- Visitor Information ----</b>
";
 echo "<img src='gambar/2.bmp'/> ".$_SERVER['REMOTE_ADDR']." (Your IP)
";
 switch($browser){
 case "Firefox" : echo "<img src='gambar/browser/firefox.gif' width='15px' height='15px'/> ".$browser." (Your Browser)
"; break;
 case "Chrome/Opera" : echo "<img src='gambar/browser/chrome.gif' width='15px' height='15px'/> ".$browser." (Your Browser)
"; break;
 case "IE" : echo "<img src='gambar/browser/ie.gif' width='15px' height='15px'/> ".$browser." (Browser Anda)
"; break;
 }
 echo "<b>------ Visitor Today -------</b>
";
 echo "<img src='gambar/1.bmp'/> <b style='color:red'>".$today['today']."</b>
";
 echo "<b>------ Visitor Yesterday -------</b>
";
 echo "<img src='gambar/2.png'/> <b style='color:red'>".$yesterday['yesterday']."</b>
";
 echo "<b>------ Total visitor -------</b>
";
 echo "<img src='gambar/1.png'/> <b style='color:red'>".$sql['total']."</b>
";
 echo "</center></div>";
?>
</body>
</html>

congratulations you've managed to create a visitor counter of your own.
Share on Google Plus

About Unknown

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment
    Facebook Comment

2 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. All in all, it seems so simple if you read the guides. However, I definitely prefer to be a native application user. I am very happy to use Grape Up - Services because I know that the company's solutions are for business.

    ReplyDelete