View Single Post
Old 05-18-2005, 07:02 AM   #2 (permalink)
DJMaze
Senior Contributor
 
DJMaze's Avatar
 
Join Date: Mar 2005
Posts: 673
DJMaze is on a distinguished road
Hmm that script opens the file 4-5 times and only counts unique ip's
So let's make it a little easier.

PHP Code:
<?php

//Core Variables
$filename 'hitcounter.txt';
$startdate 'February 7, 2005';

// Now get ip 
$ip $_SERVER['REMOTE_ADDR');
$file file($filename);

//This is the guts of the counter
if (!in_array($ip$file) && $fd fopen($filename "w")) {
    
$file[] = $ip;
    
$fstring implode("\n"$file);
    
fwrite ($fd$fstring);
    
fclose($fd);
}
$hits count($file);
echo 
"<b>Visitors since, $startdate:</b> <font color=red>$hits</font>";
A little more advanced:
PHP Code:
<?php

//Core Variables
$filename 'hitcounter.php';
$startdate 'February 7, 2005';

// Now get ip
$ip $_SERVER['REMOTE_ADDR');
// Load the file
include($filename);

//This is the guts of the counter
if ((empty($file) || !in_array($ip$file)) && $fd fopen($filename "w")) {
    
$file[] = $ip;
    
$hits count($file);
    
$fstring "<?php\n\$hits = $hits;\n\$file = array(\n'".implode("',\n'"$file)."'\n);";
    
fwrite ($fd$fstring);
    
fclose($fd);
}
echo 
"<b>Visitors since, $startdate:</b> <font color=red>$hits</font>";
This second scripts write the file as a PHP file and that way if someone tries to fetch your IP list he can't.
For example look on your site at http://mydomain.tld/hitcounter.txt
DJMaze is offline   Reply With Quote