General Statistics
390 Tutorials
202 Total Votes
21 User(s) Online
699295 Views Total
3000 Registered Members
24355 Posts Total
Rooneyfeld is our newest member
About this Tutorial: Display Total and Unique Hits on Your Site
Display Total and Unique Hits on Your Site
Rated 5 / 4 Votes

Display Total and Unique Hits on Your Site

Learn how to display the total number of visits and total number of unique visits to your site using PHP and MySQL.


Date Added: December 24, 2005 [ 10990 Views - 4 Votes ] [ Rate ]

Author: Boost [ PHP Coding ] [ 10 Comment(s) ]

Display Total and Unique Hits on Your Site
CODE
//-----------------
// Recalling and Displaying The Stats
//-----------------

$count = mysql_query("SELECT * FROM $tbl_name") or die(mysql_error());
$unique = mysql_num_rows($count);

In our first line we selected our MySQL Table, if it failed, we print the MySQL error.
The next line is pretty self explanatory...It counts the number of rows in our table and stores it as $unique. Why does it work? Well remember how our if else statement worked? If the user's IP was found it simply added 1 to the visits column, if it didn't find it, we created a new row for that visitor and added 1 for the visit row, because it was their first visit. Thus, the total number of MySQL rows you have, is the same number of unique IP's that have come your site!

We can use a while statement to get the total number of visits:
CODE
while ($vi = mysql_fetch_array($count)) {
$visits = $visits + $vi['visits'];
}

?>


We're almost done now!! All we need to do is be able to display the stats!
The following code does that!
CODE
echo "Total Hits: " . $visits . "<br />";
echo "Unique Hits: " . $unique;

?>

This part of the code is the easiest to understand, it simply prints our results.

To display your stats on any page, use the following code:
CODE
<?php include("stats.php"); ?>


That Concludes this tutorial on displaying your Total Hits: and Unique Hits: on your website using PHP and MySQL. I hope you learned something in this tutorial and found it useful!

Made By: Boost
Pixel-Designz.net


Full Code: (Commented)

MySQL Query:
CODE
CREATE TABLE `stats` (
`ip` VARCHAR( 15 ) NOT NULL ,
`visits` INT NOT NULL
) TYPE = MYISAM ;


stats.php:
CODE
<?php

//---------------------
// Define Some Connection Info
//---------------------

$host = 'hostname';
$username = 'username';
$password = 'password';
$db_name = 'database_name';
$tbl_name = 'stats'; // table name (should be correct unless changed in the mysql query)


//---------------------
// Connect To the Database
// - mysql_connect connects to MySQL
// - mysql_select_db selects the db which we will insert our data into
//---------------------

$sql_connect = mysql_connect("$host", "$username", "$password") or die("MySQL Connection Failed: " . mysql_error());;
$db_connect = mysql_select_db("$db_name")or die("Could not select DB: " . mysql_error());;

//---------------------
// And now... the Script
//---------------------

// One of the handy built in functions of PHP is $_SERVER['REMOTE_ADDR']
// $_SERVER['REMOTE_ADDR'] can be used to get the visitors IP address
// The next line takes the visitors IP address and stores it in $ip
$ip = $_SERVER['REMOTE_ADDR'];

// this next line selects the IP field of our MySQL Table
// It selects the row of $ip from which above we defined
// as the visitors IP address
$fetch = mysql_query("SELECT ip FROM $tbl_name WHERE ip ='".$ip."'") or die(mysql_error());


if ( mysql_num_rows($fetch) == 0 )
// If the output of $fetch (what is suppose to select the row if it exists)
// equals 0, it means that the row does not exist.
// In that case, we must insert the visitors IP Address into the IP row of our table
// and in the visits row of our table
// The following line does that
{
   mysql_query("INSERT INTO $tbl_name(ip,visits) VALUES('$ip','1')") or die(mysql_error());
} else {
// If the users IP was found already (he has already visited the site once)
// all we need to do is add one to our visits row, next to his IP address.
// The next line does that
   mysql_query("UPDATE $tbl_name SET visits=visits+1 WHERE ip = '$ip'") or die(mysql_error());
}


//-----------------
// Recalling and Displaying The Stats
//-----------------

$count = mysql_query("SELECT * FROM $tbl_name") or die(mysql_error());
// The above line selects our MySQL Table, if it fails, it prints the MySQL error
$unique = mysql_num_rows($count);
// The above line is pretty self explanatory...
// It counts the number of rows in our table and stores it as $unique
// Why does it work? Well remember how our if else statement worked?
// If the user's IP was found it simply added 1 to the visits row, if it didn't find it,
// we created a new field for that visitor and added 1 for the visit row, because it was their first visit.
// Thus, the total number of MySQL rows, is the total number of unique IP's that have come to your site.


// We can use a while statement to get the total number of visits
while ($vi = mysql_fetch_array($count)) {
$visits = $visits + $vi['visits'];
}

echo "Total Hits: " . $visits . "<br />";
echo "Unique Hits: " . $unique;

?>



Display the Stats on any page:
CODE
<?php include("stats.php"); ?>

Total Pages (4)
<<Prev 1 2 3 [4] 

Welcome User
Welcome Guest
( Login | Register )

Latest Tutorial Index - RSS Feed
Latest Resources - RSS Feed
Latest Tutorials - RSS Feed
Vote for Us
  • VixxIMG.com - Free Image Hosting, Free Myspace Image Hosting, Free Image Uploads
  • PageRank Plus - Google PageRank checking, with a twist.
Random Affilaites