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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.