Here Is The Counter I Use
This COUNTER script only increments once per IP address and only on the INDEX page. If you include your IP address it will not count you.
Here is a FUNCTION I use for a page counter in all my scripts.
Code:
/*
****************************************
Function page_hits
****************************************/
#----- http://www.iwcomm.com/ -----#
function page_hits(){
#-----Finds Path To File-----#
@session_start();
$exp=explode('/',$_SERVER['SCRIPT_FILENAME']);
$ref='';
$n=4;
while(!strpos($exp[$n],'php')){
$ref.= '../';
$n++;
}
#-----For Testing -----#
//echo 'site_root = '.$_SERVER["DOCUMENT_ROOT"].'/include/hitcounter.dat<br />';
//$COUNT_FILE = $ref."include/hitcounter.dat";
#-----Path To Data File-----#
$COUNT_FILE = $_SERVER["DOCUMENT_ROOT"]."/inc/hitcounter.dat";
#-----Make Sure Data File Exists-----#
if (file_exists($COUNT_FILE)){
#-----Opens The Data File And Gets Current Value-----#
$fp = fopen("$COUNT_FILE", "r+");
flock($fp, 1);
$count = fgets($fp, 4096);
#-----Check To See If It Is OK To Increment-----#
if(($_SERVER['REMOTE_ADDR'] != $_SESSION['SERVER_IP'] && $_SERVER['REMOTE_ADDR'] != $_SESSION['CURRENT_IP']) && ($_SESSION['PAGE']=='/index.php')){
#-----Increment The Data File-----#
$count += 1;
#-----Set The Current Users IP Address-----#
$_SESSION['CURRENT_IP'] = $_SERVER['REMOTE_ADDR'];
}
#-----Update And Close The Data File-----#
fseek($fp,0);
fputs($fp, $count);
flock($fp, 3);
fclose($fp);
}else{
#-----Set Displayed Counter Value If An Error Occured-----#
$count = 'Many';
}
#-----Set Count Session Value And Return $count Values
$_SESSION['count'] = $count;
return $count;
}
Variables Used:
$COUNT_FILE - Name of the counter data text file (chmod 666) on server
$_SERVER["DOCUMENT_ROOT"] - Your server base path
$_SERVER['REMOTE_ADDR'] - IP address of user connecting to your server
$_SESSION['SERVER_IP'] - Your local IP address (when connecting to the server)
$_SESSION['CURRENT_IP'] - Set by script to prevent duplicate count per session
$_SESSION['PAGE'] - Will only count hits on this page
$_SESSION['count'] - Current counter value
Usage:
I call it from a page "footer" file, so it appears on all pages.
Code:
<?page_hits()?><?=COUNT_SITE_HAS?> <?=$_SESSION['count']?> <?=COUNT_VISITORS?>
Looks Like:
This Site Has Had 244 Visitors Since January 1st, 2007
Bookmarks