Displaying Unique Visitors
Learn how you can show how many unique visitors your site has. Due to popular demand, I've made up my mind to make this tutorial.
Step 1: Requirements
Other than having a host which supports PHP and MySQL, you should have read and understood the following tutorials:
Being familiar with the said tutorials will help you in comprehending this one. It is vital that you know the first one because this tutorial is based on that.
STEP 2: Editing index.php
The index.php file is in reference to the Visitor Log With MySQL tutorial. We basically start off with the same MySQL dump and then change parts of the index.php file. Here's what it should look like:
<?php // A: DATABASE INFORMATION $user="username"; $host="localhost"; $password="password"; $database="database_name"; // B: CONNECTING TO YOUR DATABASE $connection = mysql_connect($host,$user,$password) or die ("Couldn't connect to server."); $db = mysql_select_db($database,$connection) or die ("Couldn't select database."); // C1: GETS TODAY'S DATE // SEE Date And Time TUTORIAL $today = date("Y-m-d"); // C2: GETS IP ADDRESS OF VISITORS $ip = $_SERVER['REMOTE_ADDR']; // D: CHECKS IF IP OF VISITOR WAS LOGGED TODAY $check = mysql_query("SELECT * FROM Log WHERE IP='$ip' AND Visit LIKE '%$today%'"); $unique = mysql_num_rows($check); // E: IF IP WAS LOGGED, IT WILL DISPLAY index2.php // SEE PHP Includes if ($unique > 0) { include ('index2.php'); } // F: IF IP WASN'T LOGGED, IT WILL RECORD VISITOR INFO // THEN DISPLAY index2.php (SEE PHP Includes) else { // GETS THE SITE THAT REFERRED YOUR VISITOR $referer = $_SERVER['HTTP_REFERER']; // GETS THE BROWSER YOUR VISITOR IS USING $browser = $_SERVER['HTTP_USER_AGENT']; // INSERTS VISITOR INFORMATION TO THE DATABASE $insert = mysql_query("INSERT INTO Log VALUES ('',now(),'$ip','$referer','$browser')"); // DISPLAYS index2.php include ('index2.php'); } ?>
Replace the underlined items with your own information. Sections A and B hasn't changed and there are comments to explain each section of the code.
Step 3: Displaying Unique Visitors
You can place this code in any part of your site that you want. For our mutual convenience, I placed it in index2.php so that it'll be easier for you to see how it works once you've tried it out. Here's what it should look like:
<html> <head> <title>Main Page</title> </head> <body> <?php // A: DATABASE INFORMATION $user="username"; $host="localhost"; $password="password"; $database="database_name"; // B: CONNECTING TO YOUR DATABASE $connection = mysql_connect($host,$user,$password) or die ("couldn't connect to server"); $db = mysql_select_db($database,$connection) or die ("Couldn't select database"); // C: GETS TODAY'S DATE // SEE Date And Time TUTORIAL $today = date("Y-m-d"); // D: CHECKS ALL THE VISITORS $query = "SELECT * FROM Log"; $result = mysql_query($query); // E: COUNTS TOTAL NUMBER OF VISITORS $total = mysql_num_rows($result); // F: CHECKS TODAY'S VISITORS $query .= " WHERE Visit LIKE '%$today%'"; $result = mysql_query($query); // G: COUNTS VISITS TODAY $unique = mysql_num_rows($result); // H: DISPLAYS UNIQUE AND TOTAL NUMBER OF VISITORS echo "<p>There are $unique visitors today and $total total hits.</p>"; ?> </body> </html>
As always, changed the underlined text with whichever you want. The comments in each section will guide you in what the code means. The code above should display something like this:
There are 8 visitors today and 243 total hits.
Of course, you can always change the format to whatever you like.
Conclusion
Don't you just love statistics? Try it out for yourself and see how it works for you. Enjoy!
If you have any questions or if you want to discuss this tutorial, you may post at Blinding Light MB.