Free PHP Hit Counter Script
Possible Database Tables:
| Basic Hit Counter | Unique IP Hits Counter |
CREATE TABLE hits (
page VARCHAR(30) NOT NULL,
counter INT UNSIGNED,
PRIMARY KEY (page)
);
|
CREATE TABLE ip_hits (
page VARCHAR(30) NOT NULL,
ip VARCHAR(30) NOT NULL,
counter INT UNSIGNED,
PRIMARY KEY (page, ip)
);
|
| Cookied User Hits Counter | Unique IP & Referring Page Hit Counter |
CREATE TABLE ur_hits (
page VARCHAR(30) NOT NULL,
ref VARCHAR(40) NOT NULL,
user_id INT UNSIGNED,
counter INT UNSIGNED,
PRIMARY KEY (page, user_id)
);
|
CREATE TABLE u_hits (
page VARCHAR(30) NOT NULL,
user_id INT UNSIGNED,
counter INT UNSIGNED,
PRIMARY KEY (page, user_id)
);
|
Line By Line: hits.php
This is the basic hit counter explained...
- Connect to the mysql database using your username ("username") on the local computer ("localhost") using your password ("passwd").
<? $db = mysql_connect("localhost", "username", "passwd");
- Define that you want to access database "dbname". (You can have many databases - all with different names.)
mysql_select_db("dbname",$db);
- Increment the row in the database table 'hits', where the page column is the same as the current page... (Note: $PHP_SELF is a special variable in PHP which always contains the name of the current file being processed (i.e. 'index.php')
mysql_query("UPDATE hits SET counter=counter+1 WHERE page = '$PHP_SELF'",$db);
- If no row was affected by the last update statement, then there must have been no row where the page column = $PHP_SELF. In other words, this page hasn't been hit before, so we just need to add it to the table. We use an INSERT statement to insert the page name and '1' for the number of hits.
if (mysql_affected_rows($db) < 1) { $result = mysql_query("INSERT INTO dyphits VALUES ('$PHP_SELF', 1)", $db); }
- The final statement closes the connection to the database.
mysql_close($db); ?>
NOTES...
If you want to reset your hit counter, you just delete the appropriate rows from the database. So to delete the hits for index.php you'd issue the following SQL statement in the mysql terminal window (see the page on SQL for more info!):
delete from hits where page="./index.php";
To reset the hits for all pages:
delete from hits;
Unique IP Tracking
The IP address of your visitor used to be a static value, but due to dynamic host addressing (DHCP) and the way some ISP's operate, it's no longer a good indicator of uniqueness.
| IP Address is the network address of a computer. It's a basic part of the Internet Protocol which is how packets of data know where to go... and where they're from! |
You can capture the broadcast IP of the visitor using the PHP getenv() function which gets data from the Unix environment (or shell) these are called environment variables. You need to get the REMOTE_ADDR:
$ip=getenv(REMOTE_ADDR);
The code to store the data becomes:
mysql_query("UPDATE uhits SET counter=counter+1 WHERE page = '$PHP_SELF' AND ip=\"$ip\" ",$db);
The code to retrieve the data becomes:
$result = mysql_query("SELECT page, count(ip) FROM uhits GROUP BY page",$db);
Cookied User Tracking
This is the most reliable method of tracking unique visitors. Most browser programs like Internet Explorer can be set to not accept cookies however and people may occasionally delete all the cookies on their computer...
There is no perfect way of tracking visitors but short of forcing everyone to login, I guess this is about the best.
To set a cookie on a visitor's computer that's valid for 10 years you put the following code right at the top of all your pages - before any HTML.
The get_new_user() and record_hit() functions are pseudo-functions i.e. They don't exist yet - you'll have to create them!. See the PHP manual for more info.
<?
if (!isset($user_id)) {
$user_id=get_new_user();
setcookie ("user_id", $user_id, time()+315360000, "", "yourdomain.co.uk", 0);
}
record_hit($user_id, $PHP_SELF);
?>
Once cookied, every time that browser opens a page on your website, the user_id value will be available in the page for you to use in your code...
The code to store the data becomes:
mysql_query("UPDATE uhits SET counter=counter+1 WHERE page = '$PHP_SELF' AND user_id=\"$user_id\" ",$db);
The code to retrieve the data becomes:
$result = mysql_query("SELECT page, count(ip) FROM uhits GROUP BY user_id",$db);
Referring Page Tracking
It's really essential to know where your visitors came from...
This is so easy to implement in PHP, it just takes the inclusion of one simple variable which is always available to all your scripts as it's one of the environment variables: HTTP_REFERER. Yes it is supposed to be spelt like that!
Your hit counter code becomes this:
mysql_query("UPDATE uhits SET counter=counter+1
WHERE page = '$PHP_SELF' AND ip='$ip' AND ref='$HTTP_REFERER' ",$db);
if (mysql_affected_rows($db) < 1) {
$result = mysql_query("INSERT INTO uhits VALUES ('$PHP_SELF', '$ip', '$HTTP_REFERER', 1)", $db);
}
Warning: Cannot modify header information - headers already sent by (output started at /home/sites/web-bureau.com/public_html/modules/free-php-hit-counter-script.php:7) in /home/sites/web-bureau.com/public_html/footer.php on line 12
|