PHP unique hit counter

Status
Not open for further replies.

nunoabc

New Member
Messages
151
Reaction score
0
Points
0
Here's the thing: I want to have a unique hit counter in my page. I found a script but it isn't unique... It stores all IPs from everyone, but if I reload it again, and again, and again it will count. I need a condition to check all IPs and compare to the visitors IP. If it is in the text file then it won't store it, but if isnt there it will store the IP.

PHP:
<?php
$filename = "hits.txt";

$file = file($filename);
$file = array_unique($file);
$hits = count($file);
echo $hits;

$fd = fopen ($filename , "r");
$fstring = fread ($fd , filesize ($filename));
fclose($fd);
$fd = fopen ($filename , "w");
$fcounted = $fstring."\n".getenv("REMOTE_ADDR");
$fout= fwrite ($fd , $fcounted );
fclose($fd);
?>
I'll pay 200 credits for the PHP script that best fits for me. ;)
 

TechAsh

Retired
Messages
5,853
Reaction score
7
Points
38
Try this code:
PHP:
<?php
// Unique Hits PHP Script
// ----------- March 2004
// Contact author: uniquehits@sizzly.com

$log = 'hits.log';

$IP = getenv (REMOTE_ADDR);
$add = true;
$hits = 0;

if (!file_exists ($log)) {
    echo "Error: $log does not exist.";
    exit;
}

$h = fopen ($log, 'r');
while (!feof ($h)) {
    $line = fgets ($h, 4096);
    $line = trim ($line);
    if ($line != '')
        $hits++;

    if ($line == $IP)
        $add = false;
}

fclose($h);

if ($add == true) {
    $h = fopen ($log, 'a');
    fwrite($h, "
$IP");
    fclose($h);
    $hits++;
}

echo $hits;
?>
Just include in the file that you want to monitor.
I have tested it and it works.

NOTE: I did not write this script. It came from: http://www.sizzly.com/uniquehits.htm
 

nunoabc

New Member
Messages
151
Reaction score
0
Points
0
Thank you, but as you just searched and did not made all the work I'll give you only 50 credits. I think that this is fair enough. 50 credits sent.
 

LHVWB

New Member
Messages
1,308
Reaction score
0
Points
0
* Thread closed because issue has been resolved *
 
Status
Not open for further replies.
Top