vxdesigns Posted January 22, 2007 Share Posted January 22, 2007 Hey people how's life treating ya, i'm hoping everyone's having a good weekend.I have a very simple PHP stats script which counts " Page hits, Unique hits, Todays Page hits, Todays unique hits" Here's the coding for it[code]<body><font face="Verdana" size="1"><?php// Our log file;$counter = "stats.txt";// Date logging;$today = getdate();$month = $today[month];$mday = $today[mday];$year = $today[year];$current_date = $mday . $month . $year;// Log visit;$fp = fopen($counter, "a");$line = $REMOTE_ADDR . "|" . $mday . $month . $year . "\n";$size = strlen($line);fputs($fp, $line, $size);fclose($fp);// Read log file into array;$contents = file($counter);// Total hits;$total_hits = sizeof($contents);// Total hosts;$total_hosts = array();for ($i=0;$i<sizeof($contents);$i++) { $entry = explode("|", $contents[$i]); array_push($total_hosts, $entry[0]);}$total_hosts_size = sizeof(array_unique($total_hosts));// Daily hits;$daily_hits = array();for ($i=0;$i<sizeof($contents);$i++) { $entry = explode("|", $contents[$i]); if ($current_date == chop($entry[1])) { array_push($daily_hits, $entry[0]); }}$daily_hits_size = sizeof($daily_hits);// Daily hosts;$daily_hosts = array();for ($i=0;$i<sizeof($contents);$i++) { $entry = explode("|", $contents[$i]); if ($current_date == chop($entry[1])) { array_push($daily_hosts, $entry[0]); }}$daily_hosts_size = sizeof(array_unique($daily_hosts));?><? echo "Page hits:<b> " . $total_hits . "</b><br><br>Unique hits: <b> " . $total_hosts_size . "</b><br><br>Todays Page hits: <b> " . $daily_hits_size . "</b><br><br>Todays unique hits: <b>" . $daily_hosts_size; ?>[/code]It counts the page hits properly but it does not count the unique page hits properly meaning it doesn't count the unique page hits after the first unique hit although i;ve asked people in different areas with different ips to visit the page so i can be sure that something is wrong with the code.Page hits: 10Unique hits: 1Todays Page hits: 10Todays unique hits: 1You see it keeps counting page hits but doesn't count uniques after the first one. I'm assuming somethings wrong with the coding, so please can someone take a look at the following coding and provide a fix ? I will greatly greatly appreciate it ! Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 22, 2007 Share Posted January 22, 2007 Can you post the contents of the files? Quote Link to comment Share on other sites More sharing options...
vxdesigns Posted January 23, 2007 Author Share Posted January 23, 2007 [quote author=jesirose link=topic=123455.msg510296#msg510296 date=1169441281]Can you post the contents of the files?[/quote]That's all the content, i will repost them.There are two files1)index1.php2)stats.txtCode for index1.php[code]<body><font face="Verdana" size="1"><?php// Our log file;$counter = "stats.txt";// Date logging;$today = getdate();$month = $today[month];$mday = $today[mday];$year = $today[year];$current_date = $mday . $month . $year;// Log visit;$fp = fopen($counter, "a");$line = $REMOTE_ADDR . "|" . $mday . $month . $year . "\n";$size = strlen($line);fputs($fp, $line, $size);fclose($fp);// Read log file into array;$contents = file($counter);// Total hits;$total_hits = sizeof($contents);// Total hosts;$total_hosts = array();for ($i=0;$i<sizeof($contents);$i++) { $entry = explode("|", $contents[$i]); array_push($total_hosts, $entry[0]);}$total_hosts_size = sizeof(array_unique($total_hosts));// Daily hits;$daily_hits = array();for ($i=0;$i<sizeof($contents);$i++) { $entry = explode("|", $contents[$i]); if ($current_date == chop($entry[1])) { array_push($daily_hits, $entry[0]); }}$daily_hits_size = sizeof($daily_hits);// Daily hosts;$daily_hosts = array();for ($i=0;$i<sizeof($contents);$i++) { $entry = explode("|", $contents[$i]); if ($current_date == chop($entry[1])) { array_push($daily_hosts, $entry[0]); }}$daily_hosts_size = sizeof(array_unique($daily_hosts));?><? echo "Page hits:<b> " . $total_hits . "</b><br><br>Unique hits: <b> " . $total_hosts_size . "</b><br><br>Todays Page hits: <b> " . $daily_hits_size . "</b><br><br>Todays unique hits: <b>" . $daily_hosts_size; ?>[/code]The stats.txt file is empty. I took this script from www.zymic.com and you can download it by following this linkhttp://zymic.com/download_download.php?id=23You can see this script in action herehttp://www.pwnagetime.net/test/index1.phpNow the issue as i've mentioned before is that the script keeps counting regular hits/refreshes but doesn't count a unique hit after the first one. I've asked my friends to visit the test page and also used different proxy services to view the page with a new ip and still it doesn't count the unique hit. Zymic forums are down and their irc channel is good as dead, so i seek help from the professionals. Please help me out and i will love you forever ! Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 23, 2007 Share Posted January 23, 2007 It can't be empty if it's showing anything. It may have been empty when you uploaded it, but it has been written to and isn't empty anymore. I wanted to see that file. Quote Link to comment Share on other sites More sharing options...
vxdesigns Posted January 23, 2007 Author Share Posted January 23, 2007 [quote author=jesirose link=topic=123455.msg511139#msg511139 date=1169530609]It can't be empty if it's showing anything. It may have been empty when you uploaded it, but it has been written to and isn't empty anymore. I wanted to see that file.[/quote]This is what's inside the stats.txt file[code]|23January2007|23January2007|23January2007|23January2007|23January2007|23January2007|23January2007|23January2007|23January2007|23January2007[/code]Hope this can help you figure out what's wrong :/ Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 23, 2007 Share Posted January 23, 2007 It's not storing any hosts, or there would be something before the | <- Pipe.Here is the code I am talking about[code]$total_hosts = array();for ($i=0;$i<sizeof($contents);$i++) { $entry = explode("|", $contents[$i]); array_push($total_hosts, $entry[0]);}$total_hosts_size = sizeof(array_unique($total_hosts));[/code]This explodes the info based on the | - whatever is before the | is the host. None of yours have anything before the | so it is all the same (or none?) host.$REMOTE_ADDR is not working. Where does that var come from? Here is the script I use to get an IP address:[code]if(!isset($_SERVER["HTTP_X_FORWARDED_FOR"])){ $ip=$_SERVER["REMOTE_ADDR"];}else{ $ip=$_SERVER["HTTP_X_FORWARDED_FOR"];}[/code] Quote Link to comment Share on other sites More sharing options...
vxdesigns Posted January 23, 2007 Author Share Posted January 23, 2007 I think it's getting the host/ip info by the following method// Log visit;$fp = fopen($counter, "a");[b]$line = $REMOTE_ADDR . "|" . $mday . $month . $year . "\n";[/b]$size = strlen($line);fputs($fp, $line, $size);fclose($fp);Do you see any problem with that ? How can i implement your method of getting ip/host information into this script ?One more thing i'd like to know is that can you download this script from herehttp://zymic.com/download_download.php?id=23And test it on your host to see if it works because lately alot of simple scripts have started to give me error on my new host, so i want to make sure that if it's my host who has disabled something or not because of which i'm running into issues.Thanks in advance Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 23, 2007 Share Posted January 23, 2007 No, I'm not going to download it and put it on my machine. Try what I posted and change $REMOTE_ADDR to $ip. Quote Link to comment Share on other sites More sharing options...
vxdesigns Posted January 23, 2007 Author Share Posted January 23, 2007 So where exactly do i put this code ???[code]if(!isset($_SERVER["HTTP_X_FORWARDED_FOR"])){ $ip=$_SERVER["REMOTE_ADDR"];}else{ $ip=$_SERVER["HTTP_X_FORWARDED_FOR"];}[/code]Shall i do it like this// Log visit;$fp = fopen($counter, "a");$line = if(!isset($_SERVER["HTTP_X_FORWARDED_FOR"])){ $ip=$_SERVER["REMOTE_ADDR"];}else{ $ip=$_SERVER["HTTP_X_FORWARDED_FOR"];} "|" . $mday . $month . $year . "\n";$size = strlen($line);fputs($fp, $line, $size);fclose($fp); Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 23, 2007 Share Posted January 23, 2007 No. You might want to read some basics tutorials, you can't assign an if statement to a var. (AFAIK!)[code]if(!isset($_SERVER["HTTP_X_FORWARDED_FOR"])){ $ip=$_SERVER["REMOTE_ADDR"];}else{ $ip=$_SERVER["HTTP_X_FORWARDED_FOR"];}// Log visit;$fp = fopen($counter, "a");$line = $ip . '|' . $mday . $month . $year . "\n";$size = strlen($line);fputs($fp, $line, $size);fclose($fp);[/code] Quote Link to comment Share on other sites More sharing options...
vxdesigns Posted January 23, 2007 Author Share Posted January 23, 2007 Thx alot it works now :/Please do tell me which one would you prefer txt based scripts or mysql based ? I mean security vise and code optimization wise and code executing wise ? Like which hit counter would you recommend txt based or mysql based ? Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 23, 2007 Share Posted January 23, 2007 It depends on what you're doing. For a hit counter, text is fine. Quote Link to comment Share on other sites More sharing options...
vxdesigns Posted January 23, 2007 Author Share Posted January 23, 2007 No i mean lets say this same script comes in two forms one txt and one mysql then which one would you use ? Quote Link to comment Share on other sites More sharing options...
vxdesigns Posted January 27, 2007 Author Share Posted January 27, 2007 Okie i think i found another bug/issue with the script. The problem is that it stops counting unique hits after the first 5 unique hits whereas it still keeps counting page refreshes/hits. What could be causing this ??? Any fix will be greatly appreciated ! Quote Link to comment Share on other sites More sharing options...
unstopabl3 Posted January 28, 2007 Share Posted January 28, 2007 I'm having the same issue. Any updates on this ? Quote Link to comment Share on other sites More sharing options...
redarrow Posted January 28, 2007 Share Posted January 28, 2007 use this link okhttp://devzone.zend.com/node/view/id/1273 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.