VB icon

Custom website Stats and Hits counter - MySQL and PHP

Email
Submitted on: 1/1/2015 4:47:00 AM
By: David Good (from psc cd)  
Level: Beginner
User Rating: By 6 Users
Compatibility: PHP 4.0
Views: 6682
 
     Custom code records basic statistics (IP, Host, Referrer, and UserAgent) and hits count for unique visitors to your website. Tries to prevent duplicate entries.
 
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
//**************************************
// for :Custom website Stats and Hits counter - MySQL and PHP
//**************************************
// David Good - 2000
// clannagael@gmail.com
// Feel free to use and/or modify
// this code however you see fit.
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: Custom website Stats and Hits counter - MySQL and PHP
// Description:Custom code records basic statistics (IP, Host, Referrer, and UserAgent) and hits count for unique visitors to your website. Tries to prevent duplicate entries.
// By: David Good (from psc cd)
//
// Assumes:ASSUMES: 
//MySQL is installed and configured
//PHP is installed as an Apache Module
//You have access to Apache environment variables
//**************************************

// REQUIRED: 2 separate tables:
// Create table statements:
// CREATE TABLE hits(count int not null default 0);
// CREATE TABLE stats(id int Primary Key Not null default 0 auto_increment, ipaddress varchar(15), host varchar(150), browser varchar(100), referer varchar(200));
//For demonstration purposes we'll assume the
//database is called Statistics, You're
//connecting from localhost with a user id
//"user" and a password of "password"
//
//Set database name:
$dbname = "Statistics";
//
//Get environment variables that
//we'll use for stats:
$ip = getenv("REMOTE_ADDR");
$host = getenv("HTTP_HOST");
$browser = getenv("HTTP_USER_AGENT");
$referer = getenv("HTTP_REFERER");
//
//Connect to MySQL - the @
//sign tells php to suppress
//mysql errors in favor of
//the "..or die.." clause
$cn = @mysql_connect("localhost", "user", "password") or die("Couldn't connect to SQL server");
//
//Select the database:
$db = @mysql_select_db($dbname, $cn) or die("Couldn't Access database: $dbname");
//
//Create our validation sql here -
//This query will allow us to log
//ONLY unique visitors
//and NOT duplicates or people
//that refresh the page 83 times
$sql = "SELECT ipaddress FROM stats WHERE ipaddress = \"$ip\"";
//
//Execute query:
$result = @mysql_query($sql, $cn) or die("Couldn't execute query");
//
//See if there were any matches
//by checking for an empty recordset
$num = mysql_numrows($result);
//
//If there were 0 results
//for that query then we're PRETTY
//sure that this is a unique
//visitor( We can't be POSITIVE
//because dial-up accounts will
//change ip's on us - at least
//if they refresh the page, we're not
//updating our hits counter every time)
if( $num == 0 ) {
//Create a uniqe stats entry:
$sql = "INSERT INTO stats(ipaddress, host, browser, referer) VALUES(\"$ip\", \"$host\", \"$browser\", \"$referer\")";
//
$result = @mysql_query($sql, $cn) or die("Couldn't update stats");
//
//Now update our hits count:
$sql = "UPDATE hits SET count = count + 1";
$result = @mysql_query($sql, $cn) or die("Couldn't update hits");
//
//You could even email yourself a notification
//that you've had a new visitor
mail("youremail@yourdomain.com", "You had a new visitor", "From $ip\nHost: $host", "FROM: stats@yourwebsite.com");
}


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.