Last updated on November 8th, 2024 at 12:33 pm
This is an advanced yet straightforward graphical hit counter developed using PHP and MySQL. A key benefit of this script is its ability to dynamically alter the counter’s appearance. The only requirement is to have numeric image files ranging from 0 to 9, named according to the convention 0.jpg, 1.jpg, 2.jpg, and so forth, up to 9.jpg. Each style can be stored in different directories.
The counter operates by utilizing cookies, which are configured to expire after one day. Additionally, we can enhance its reliability by recording the user’s IP address in the database, which will be elaborated upon in a separate tutorial in the future.
The first step is the create a MySQL database with a table inside that. After that we are inserting counter as value as 0 to start with.
CREATE DATABASE `counter` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci;
USE `counter`;
CREATE TABLE IF NOT EXISTS `counter_data` (
`counter` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO counter_data VALUE ='0';
The second step is to create a file, counter.php and add the below code. Modify the Mysql values accordingly.
PHP 5 & Above
<?php
#Create cookie with 1 day expiry
$expire=time()+86400;
$dir_name=$_GET['counterdir'];
if((!isset($_GET['counterdir'])) || ($dir_name ==""))
{
echo "Please provide the directory path";
exit;
}
#Connect to database
$con=mysqli_connect("localhost", "", "") ;
mysqli_select_db($con,"counter");
$result = mysqli_query($con,"SELECT * FROM counter_data");
$row = mysqli_fetch_array($result);
$value = $row['counter'];
#Check whether the cookie has been set
if (!isset($_COOKIE["hit"]))
{
$add = $value+1;
$update = mysqli_query($con,"update counter_data set counter=$add");
echo "You are visitor number $add!!!<br>";
$display = $add;
setcookie("hit", "counter", $expire);
}
else
{ echo "You have already visited this webpage. Counter still $value!!<br>";
$display = $value;
}
$a=str_split($display);
$count = count($a);
$i=0;
for($i==0;$i<$count;$i++)
{
switch($a[$i]){
case "0":
echo "<img src=$dir_name/Counter-0.png>";
break;
case "1":
echo "<img src=$dir_name/Counter-1.png>";
break;
case "2":
echo "<img src=$dir_name/Counter-2.png>";
break;
case "3":
echo "<img src=$dir_name/Counter-3.png>";
break;
case "4":
echo "<img src=$dir_name/Counter-4.png>";
break;
case "5":
echo "<img src=$dir_name/Counter-5.png>";
break;
case "6":
echo "<img src=$dir_name/Counter-6.png>";
break;
case "7":
echo "<img src=$dir_name/Counter-7.png>";
break;
case "8":
echo "<img src=$dir_name/Counter-8.png>";
break;
case "9":
echo "<img src=$dir_name/Counter-9.png>";
break;
default:
echo "None";
}
}
?>
PHP 5 or older version (without using mysqli function)
<?php
#Create cookie with 1 day expiry
$expire=time()+86400;
$dir_name=$_GET['counterdir'];
#Connect to database
mysql_connect("localhost", "", "") or die(mysql_error());
mysql_select_db("counter") or die(mysql_error());
$result = mysql_query("SELECT * FROM counter_data");
$row = mysql_fetch_array($result);
$value = $row['counter'];
#Check whether the cookie has been set
if (!isset($_COOKIE["hit"]))
{
$add = $value+1;
$update = mysql_query("update counter_data set counter=$add");
echo "You are visitor number $add!!!<br>";
$display = $add;
setcookie("hit", "counter", $expire);
}
else
{ echo "You have already visited this webpage. Counter still $value!!<br>";
$display = $value;
}
$a=str_split($display);
$count = count($a);
$i=0;
for($i==0;$i<$count;$i++)
{
switch($a[$i]){
case "0":
echo "<img src=$dir_name/0.jpg>";
break;
case "1":
echo "<img src=$dir_name/1.jpg>";
break;
case "2":
echo "<img src=$dir_name/2.jpg>";
break;
case "3":
echo "<img src=$dir_name/3.jpg>";
break;
case "4":
echo "<img src=$dir_name/4.jpg>";
break;
case "5":
echo "<img src=$dir_name/5.jpg>";
break;
case "6":
echo "<img src=$dir_name/6.jpg>";
break;
case "7":
echo "<img src=$dir_name/7.jpg>";
break;
case "8":
echo "<img src=$dir_name/8.jpg>";
break;
case "9":
echo "<img src=$dir_name/9.jpg>";
break;
default:
echo "None";
}
}
?>
Invoking the aforementioned script is quite straightforward. If your website is named somewebsite.com and the folders for the counter styles, such as style1, style2, etc., are located in the root directory, you simply need to append the URL with the appropriate query string.
For instance, you can use http://somewebsite.com/counter.php?counterdir=style1 or http://somewebsite.com/counter.php?counterdir=style2, depending on your preferred style. To incorporate this page into any PHP script, you would use the following command: include(‘counter.php?counterdir=style1’);