shortysbest Posted April 4, 2010 Share Posted April 4, 2010 My php unique view counter works and all, but on my localhost it only writes my ip once and counts the view once and then doesnt anymore no matter how many times i refresh the page, but when i put it on my hosting server it counts my ip and other ip's over and over everytime it's viewed and it adds to the view counter. I'm hoping someone could help me out here. the entire code is: <?php //open ip addres ip, count line $ipfile = file("ips.txt"); $linecount = count($ipfile); //open count file $file = file_get_contents("count.txt"); $visitors = $file; // set incremement boolean (should we add 1?) $increment = true; //users ip $ip = $_SERVER['REMOTE_ADDR']; if ($linecount==0) $increment == true; //start loop for($x=0;$x<$linecount;$x++) { //does ip match that in file? if(intval($ipfile[$x])==$ip) $increment = false; } //open new file, write new visitors value if ($increment == true) { $filenew = fopen("count.txt",'w'); $visitorsnew = $visitors + 1; fwrite($filenew,$visitorsnew); $filenewip = fopen("ips.txt",'a'); fwrite($filenewip,$ip."\n"); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/197518-php-unique-view-counter-works-correctly-on-localhost-not-on-server-though/ Share on other sites More sharing options...
Tazerenix Posted April 4, 2010 Share Posted April 4, 2010 This way should work. Not sure what the intval() was for. Hope this works. I havent tested it. <?php error_reporting(E_ALL); $ipfile = file_get_contents("ips.txt"); // Put ips.txt into string $lines = explode("\r\n", $ipfile); // Split ip file into array at each new line (might be \r\n) $file = file_get_contents("count.txt"); // get count file $visitors = (int)$file; // Set to int // set incremement boolean (should we add 1?) $increment = true; //users ip $ip = $_SERVER['REMOTE_ADDR']; if (!$lines) { $increment = true; } //start loop foreach($lines as $line) { if($line == $ip) { $increment = false; } } //open new file, write new visitors value if ($increment == true) { $visitorsnew = $visitors + 1; file_put_contents("count.txt", $visitorsnew); file_put_contents("ips.txt", $ip . "\n", FILE_APPEND); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/197518-php-unique-view-counter-works-correctly-on-localhost-not-on-server-though/#findComment-1036656 Share on other sites More sharing options...
ignace Posted April 4, 2010 Share Posted April 4, 2010 intval() converts the input to an integer thus 192.186.1.1 would return 192 I doubt that that was what the OP had in mind when he wrote it He probably was hoping it secretly called ip2long Quote Link to comment https://forums.phpfreaks.com/topic/197518-php-unique-view-counter-works-correctly-on-localhost-not-on-server-though/#findComment-1036673 Share on other sites More sharing options...
shortysbest Posted April 4, 2010 Author Share Posted April 4, 2010 This way should work. Not sure what the intval() was for. Hope this works. I havent tested it. <?php error_reporting(E_ALL); $ipfile = file_get_contents("ips.txt"); // Put ips.txt into string $lines = explode("\r\n", $ipfile); // Split ip file into array at each new line (might be \r\n) $file = file_get_contents("count.txt"); // get count file $visitors = (int)$file; // Set to int // set incremement boolean (should we add 1?) $increment = true; //users ip $ip = $_SERVER['REMOTE_ADDR']; if (!$lines) { $increment = true; } //start loop foreach($lines as $line) { if($line == $ip) { $increment = false; } } //open new file, write new visitors value if ($increment == true) { $visitorsnew = $visitors + 1; file_put_contents("count.txt", $visitorsnew); file_put_contents("ips.txt", $ip . "\n", FILE_APPEND); } ?> This code still counts every view by me. it does exactly what my code did. :\ Quote Link to comment https://forums.phpfreaks.com/topic/197518-php-unique-view-counter-works-correctly-on-localhost-not-on-server-though/#findComment-1036725 Share on other sites More sharing options...
Tazerenix Posted April 5, 2010 Share Posted April 5, 2010 well another method, which im looking into, is using a database to store info about each view then query the database. But im not sure why the code didnt work so... Quote Link to comment https://forums.phpfreaks.com/topic/197518-php-unique-view-counter-works-correctly-on-localhost-not-on-server-though/#findComment-1036982 Share on other sites More sharing options...
mikesta707 Posted April 5, 2010 Share Posted April 5, 2010 using a data base would probably be alot easier. instead of looping through a file, simply do a query or 2 and your done. off topic: btw your quote "War does not determine who is right... Only who is dead" - Unknown i think it goes "War does not determine who is right... Only who is left" - Unknown Quote Link to comment https://forums.phpfreaks.com/topic/197518-php-unique-view-counter-works-correctly-on-localhost-not-on-server-though/#findComment-1036987 Share on other sites More sharing options...
shortysbest Posted April 5, 2010 Author Share Posted April 5, 2010 I actually just came up with another code that works correctly. Just it writes the ip over and over each time visited, but it doesnt change the view count variable. <?php $filename = "count.txt"; $file = file($filename); $file = array_unique($file); $hits = count($file); $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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/197518-php-unique-view-counter-works-correctly-on-localhost-not-on-server-though/#findComment-1036994 Share on other sites More sharing options...
Tazerenix Posted April 5, 2010 Share Posted April 5, 2010 off topic: btw your quote "War does not determine who is right... Only who is dead" - Unknown i think it goes "War does not determine who is right... Only who is left" - Unknown I no xD. But "Only who is dead" sounds better XD Quote Link to comment https://forums.phpfreaks.com/topic/197518-php-unique-view-counter-works-correctly-on-localhost-not-on-server-though/#findComment-1036997 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.