PHP: Simple Unique Hits Counter (Re-Vamp)

This is a revamp of one of my old tutorials. I updated the comments and directions to make it more legible, if you find problems or have comments please feel free to post them or if you want to ask me something privately please email me, you can find my email on the contact page.

This tutorial assumes that you know some PHP before we start, if you don't you might want to read some of the basic tutorials on the site.
The idea of a unique hits counter is to count how many different IPs have visited your site. We will make this counter using a MySQL database to save the IPs and then when anybody logs on to the site check if his/her IP is in the database and if it's not add it and then show the amount of hits.

The first thing you need to do is make a table to save all the IPs. To make the table open PHPmyAdmin on your server and open the SQL window and put in this code:

CREATE TABLE `users_online` (
`id` INT( 80 ) NOT NULL AUTO_INCREMENT ,
`ip` VARCHAR( 255 ) NOT NULL ,
PRIMARY KEY ( `id` ) ,
UNIQUE (
`ip`
)
);

If you don't have PHPmyAdmin or don't know how to use it then make a PHP file fill in the correct DB connection info and run the file on your server:

<?php
$host = ""; // your database host, this is usually 'localhost' (without the 's)
$user = ""; // your database user name
$pass = ""; // your database password
$db = ""// the name of your database

$dbh=mysql_connect ("$host", "$user", "$pass") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("$db");

$sql  = 'CREATE TABLE `users_online` ('
. '   `id` INT(80) NOT NULL AUTO_INCREMENT, '
. '   `ip` VARCHAR(255) NOT NULL,'
. '   PRIMARY KEY (`id`),'
. '   UNIQUE (`ip`)'
. ' )'
. ' TYPE = myisam';
if(mysql_query($sql))
{
echo "Table Created!";
}
else
{
echo "Error! Table not created! Check connection info.";
}

?>

Delete the file once you have used it to create the table.

Ok now we have a table with the fields id and ip witch will use to store our data.

Now lets make the .php files. The first file will be config.php, in this file we will keep our database conection info and we will just include the file into all our other files.

<?php
$host = ""; // your database host, this is usually localhost
$user = ""; // your database user name
$pass = ""; // your database password
$db = ""// the name of your database

$dbh=mysql_connect ("$host", "$user", "$pass") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("$db");
?>

Ok, now we'll make the main file that will check the IP and add records to the database. I will name it count.php, but it does not matter what you name it, you just have to remember the name.

<?php
include('config.php'); // include the db conection file

$ip = $_SERVER['REMOTE_ADDR']; //Get user IP
$check = mysql_query("SELECT * FROM users_online WHERE ip = '$ip'"); //Check the database for the IP
$check_ip = MYSQL_NUM_ROWS($check);
if($check_ip == 0) //If the Query shows 0 results
{
mysql_query("INSERT INTO users_online (ip) VALUES ('$ip')"); //Add the IP to the Database
}

$show = mysql_query("SELECT * FROM users_online"); //Get all the data from the database for showing the count
$show2 = MYSQL_NUM_ROWS($show); //Count the rows
echo "Unique hits: $show2"//Echo the # of unique hits
?>

Now all you need to put on your site is put this code where you want the counter to show.

<?php
include('count.php'); //Include the count file, your filename might be different
?>

Be careful if you put your file in a different directory because you might need to change the path to the file

That's all there is to a simple unique hits counter, I showed you all the basics now you can experiment and make much more sophisticated counter with functions like todays unique hits and other exiting things. Remember experimentation is key.

Leave a Reply

Sorry, comment posting is disabled untill the new CMS is done.