Jump to content
xisto Community
Sign in to follow this  
ChronicLoser

Php Problem - Unique Counter my script isnt working correctly

Recommended Posts

alright here's what I want to do: I made a counter to count unique visitors without a mysql. But the problem is that the '.dat' file I'm writing on (unique.dat) tends to become too big when I have too many visitors. Thus, I tried to add an if/then statement.

Kay, now here's the problem: even though this code successfully runs, it's not a hundred-percent accurate. Sometimes it rewrites a user's IP even though it is already on the '.dat' file. Even though it doesn't actually affect the $hits, I'd still like to know what I did wrong.

<?php$filename = "unique.dat";$file = file($filename);$file = array_unique($file);$hits = count($file);$remote = $_SERVER["REMOTE_ADDR"];$var = $_SERVER['PHP_SELF'];$var = 'unique.dat';$wholefile = file_get_contents($var);preg_match_all("|$remote|U",$wholefile, $out, PREG_PATTERN_ORDER);if(!($out[0][0] == $remote)){$fd = fopen ($filename , "a+");$fout = fwrite ($fd , "$remote\n");fclose($fd);echo "bad";}echo $hits; ?>
hope someone could help me out here ^_^

Share this post


Link to post
Share on other sites

I see one error. You aren't looping through your array of ips to check each one. you just check to see if the first on in the array matches. Next why assign 'uniqe.dat' to two seperate variables as well why set $_SERVER['PHP_SELF'] and 'unique.dat' to the same variable?

Share this post


Link to post
Share on other sites

Alright coder....i read your reply, and fixed up some things. The $_SERVER['PHP_SELF'] was completely unneccessary so i took it out. I was trying to something else earlier. The $var was also a careless mistake on my part so I took that out. I also changed around the if/then statement, but I wasn't sure what you meant when you said I was to loop through the arrays. I'm trying to match the ips using the preg_match function, so I don't think the loop would be needed...but correct me if I'm wrong cause I'm not the exactly the brightest person you could find

anyways, here's the edited code:

<?php$filename = "unique.dat";$file = file($filename);$file = array_unique($file);$hits = count($file);$remote = $_SERVER["REMOTE_ADDR"];$wholefile = file_get_contents($filename);   if (preg_match ("/$remote/i", $wholefile))    {      echo $hits;   }    else    {      $fd = fopen ($filename , "a+");      $fout = fwrite ($fd , "$remote\n");      fclose($fd);      echo $hits;   }?>

Share this post


Link to post
Share on other sites

think problem is your preg matchif (preg_match ("/$remote/i", $wholefile)) i'd go for this: if (preg_match("/^".$remote."$/i","$wholefile))this will asure that192.168.34.1 and 192.168.34.10 are detected as different addresses.same for 181.x.y.z and 81.x.y.z.but i don't know why the program would add ip's twice :smaybe if the file can't be accessed? i think that might cause the if to be false because $wholefile would be empty

Share this post


Link to post
Share on other sites

alright, i found the problem. It was cause I had a seperate file with bad coding that could also access the 'unique.dat' file. So my little php script officially works now ^_^ (and to marijnnn - I couldn't get the code you suggested to work properly because it seems to always result in false, no matter what was within the unique.dat file)

anyways, here's the final code if anyone wants to use it. You will also need a unique.dat file in the same folder and have it CHMOD to 777 ^_^

<?php$filename = "unique.dat";$file = file($filename);$file = array_unique($file);$hits = count($file);$remote = $_SERVER["REMOTE_ADDR"];$wholefile = file_get_contents($filename);   if (preg_match("/$remote/i",$wholefile))   {      echo $hits;   }    else    {      $fd = fopen ($filename , "a+");      $fout = fwrite ($fd , "$remote\n");      fclose($fd);      echo $hits;   }?>

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | 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.