Visitor Counter Script Using PHP
If you have noticed that many websites display their total numbers of visitors.In this tutorial I am going to explain you how to create a simple visitor counter using php.
For this,
1) Create one table called “visitor_counter” in your database.
2) Create file named “counter.php”.
Step1: Creating “visitor_counter” Table
CREATE TABLE `visitor_counter` ( `counts` int(10) NOT NULL default '0' )
Step2: Creating “counter.php” File
<?php
// Database Details
$host="localhost";
$username="root";
$password="";
$db_name="test";
$tbl_name="visitor_counter";
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect to server ");
mysql_select_db("$db_name")or die("cannot select DB");
$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);
$rows=mysql_fetch_array($result);
$counter=$rows['counts'];
// setting counter = 1, if we have no counts value
if(empty($counter)){
$counter=1;
$sql1="INSERT INTO $tbl_name(counts) VALUES('$counter')";
$result1=mysql_query($sql1);
}
echo "You 're visitors No. ";
echo $counter;
// Incrementing counts value
$plus_counter=$counter+1;
$sql2="update $tbl_name set counts='$plus_counter'";
$result2=mysql_query($sql2);
mysql_close();
?>
Comments
Post a Comment