Jump to content

Wonky Guest Counter


Man In Tan

Recommended Posts

Code replaced with justsomeguy's code.

This is the guest counter that has been on my site since almost its beginning. I can't see any errors, but sometimes it acts wonky. It is supposed to log each unique IP address once, so I'll know about how many people have visited. However, sometimes, it logs the same IP address twice in a row. I don't know if it logs old IP addresses again sometimes, because it is a lot of numbers it look through. I had this problem once on Apache / PHP4 and four times on Apache2 / PHP5.Does anyone see any errors I missed, or is my system just having issues?In addition, http://localhost/ used to have an IP address of 127.0.0.1 on Apache, but now its IP address appears to be"::1" on Apache2, which isn't even an IP address. But this is more of an enigma than an actual problem.EDIT: Script updated.EDIT (2): Script replaced.

Link to comment
Share on other sites

Yeah, that's a good idea. I just updated the script.It'll stop the IP counter from reaching a point where it will not read new IP addresses, but because "::1" is at the bottom of the list, but only once, I can safely say that isn't the problem.

Link to comment
Share on other sites

This may be easier to manage:

<?php$fname = '/TanServer/Database/ipdatabase.txt';$ip_list = file($fname, FILE_IGNORE_NEW_LINES);$ip = $_SERVER['REMOTE_ADDR'];if (!in_array($ip, $ip_list)){  file_put_contents($fname, file_get_contents($fname) . "\n" . $ip);}$counter = '/TanServer/Database/ipcount.txt';$num = intval(file_get_contents($counter));file_put_contents($counter, ++$num);?>

That will add the new IP to the list if it's not already there, increment the counter, and set $num to the value that the counter just got incremented to.

Link to comment
Share on other sites

Thank you! Hopefully that will stop double logging.As a side note, right after implementing your code, another weird IP address showed up ... "fe80::230:65ff:fe19:fdc3". Hmmm ..... Maybe Apache2 is having difficulties.

Link to comment
Share on other sites

All right. They may be showing up just now, because Apache 1.3 may have not been able to possess them.I'm trying to understand your code, but that last part baffles me. How does it increment the counter only for new guests, when it is not within the if(){} statement?EDIT: Upon further testing, it increments every page load. I'm moving it into the if(){} statement.

Link to comment
Share on other sites

That's right, it looked like your original code had a list of unique IP addresses, and a counter for every visit. If you just want to count the number of unique visitors you don't need another counter, you just count the number of IPs. You can get that with count($ip_list).

Link to comment
Share on other sites

No, it just looked like a separate hit counter because my code was jumbled and confusing looking.I was not aware of the file_put_contents() and file_get_contents() functions, but after seeing your code and looking them up, I realized that they were something I didn't think there was a function for, but thought there should be. I can also use that to simplify my forum script. I actually wrote custom functions to do that. :) A count() function, huh? that's neat. I just found the code to mimic my original code's other function (to display guest numbers) as well: "echo array_search($_SERVER['REMOTE_ADDR'], $ip_list) +1;".

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...