Jump to content

[SOLVED] Array_Unique


KashMoney

Recommended Posts

Hello,

 

I am trying to do a hit counter that writes an ip address to a text file. I only want ip's that are unique.

 

This is what I have;

<?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);
?>

 

Then ovbiously the hits.txt logs all of the hit counter unique ip address. The problem I have with this script is that every time I reload the number says at 3 it does add +1 every time. Any Help on that?

 

Thanks,

 

KashMoney

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/80077-solved-array_unique/
Share on other sites

I think your writing to the file after reading from it you should write to the file first and then read from it

 

 

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


$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);

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

 

Link to comment
https://forums.phpfreaks.com/topic/80077-solved-array_unique/#findComment-405832
Share on other sites

I tried your method rajivgonsalves but its saying at three for some reason. So weird!

 

At first when i reset my browser I get this mesage Warning: fread() [function.fread]: Length parameter must be greater than 0. in /home/mmaaaco/public_html/kkashou/homework/q3/counter.php on line 6

2. After I refresh again its goes back to 3. Hope this helps.

 

Anything else?

 

 

KashMoney

Link to comment
https://forums.phpfreaks.com/topic/80077-solved-array_unique/#findComment-405836
Share on other sites

to avoid that error your code should be

 

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


$fd = fopen ($filename , "a+");
fwrite ($fd , getenv("REMOTE_ADDR")."\n" );
fclose($fd);

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

Link to comment
https://forums.phpfreaks.com/topic/80077-solved-array_unique/#findComment-405843
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.