PHP : Hit Counter

3 Mar

//Hit counter by Mustals (c)2002
//This script is free to use, please leave in this blurb though.
// The script stores a visitors IP number to prevent
// repeat visitors clocking the counter.
//WHAT YOU NEED to use this script is…
//A server with PHP and SSI installed and operational
//and the ability to CHMOD your files on UNIX/LINUX severs.
//SETUP: Make two files: ip_data.dat & count.dat.
//Change the variables $IPfilePath and $countFilePath to where
//you are going to put these two files on your server.
//CHMOD them to 777 this file and 755 for the other two
//if in a UNIX environment.
//Give the webpage on which you wish to use the counter an
//extension of .shtm or .shtml, ie index.shtml.
//Put this line where you want the counter: <!–#include file=”count.php” –>
//you might have to change the path eg <!–#include file=”/cgi-bin/count.php” –>
//Using HTML tags or Styles change the output to match your site.
//For example <B><!–#include file=”count.php” –></B> will cause the
//numbers to be displayed as Bold.
//The variable $hitMessage1 & $hitMessage2 allows text to
//be placed before and after the counter. Change this to
//anything as long as the ” “; are in place, for no message
//leave the quotes empty ie $hitMessage1=””;
// There are two error messages in case bad things start to
// happen when running this script. $readErr & $writeErr,
//you may like to customise these to your own e-mail address.
//The script will store your own IP so do not expect to see
//increments when you refresh the page. To check its working
//you need to delete your IP number from the ip_data.dat file.

//Set Variables

$IPfilePath = “/home/sites/”; // Path to IP number file
$countFilePath = “/home/sites/”; //Path to Count file

$readErr = “Read error, contact for help”;
$writeErr = “Write error, contact for help”;

$hitMessage1 =”MV “;
$hitMessage2 =” hits”;

//================= No need to change anything after here ========================

$IPnum = “”; //Set as a String
$userStatus = 0;

// Get the current IP number ——————————
$IPnum = getenv(“REMOTE_ADDR”);

//Get stored IP’s from a file ——————————–

$IPlist = @fopen($IPfilePath, “r”)or die($readErr);
$IPdata = fread($IPlist,filesize($IPfilePath));

//Compare it to the ones stored in ip_data.dat —

$IParray = explode(” “,$IPdata); //Make array of IPs

// Start comparing IPs


if($IPnum != $IParray[$ipCount]){$userStatus = 0;} //New visit by IPnum
elseif($IPnum == $IParray[$ipCount]){$userStatus = 1;}//Been before
}// End for loop

// OK it’s a new visitor
// Store the IP number in case they ever come back.
// The counter, give it one.
if($userStatus == 0){
$IPfile = @fopen($IPfilePath, “a”) or die ($writeErr); // Write the new IP to ip_data.dat
@fwrite($IPfile, ” $IPnum”) or die($writeErr);

$incCount = @fopen($countFilePath, “r”) or die ($readErr);
$theCount = fread($incCount, filesize($countFilePath));

$incCount = @fopen($countFilePath, “w”) or die ($readErr);
@fwrite($incCount, $theCount) or die ($writeErr);
elseif($userStatus == 1){
$incCount = @fopen($countFilePath, “r”) or die ($readErr);
$theCount = fread($incCount, filesize($countFilePath));
echo “$hitMessage1 $theCount $hitMessage2”;


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: