squiblo Posted September 22, 2009 Share Posted September 22, 2009 with my hit counter, every time i refresh the page the hits go up by one everytime, but it should only go up by one the first time the page is viewed because the ip address is stored in a .txt file, and if the ip address is already stored then the hit should stay the same, also everytime i refresh the ip is stored again when it should only be stored once, i don't understand where i am going wrong <?php //connect to db mysql_connect("localhost","","") or die ("Couldn't connect"); mysql_select_db("") or die ("Couldn't find db"); $query = mysql_query("SELECT * FROM members WHERE pageid='$pageid'"); $row = mysql_fetch_assoc($query); //get current hits $hits = $row['counter']; //get ip file $ipfile = file("ips.txt"); $linecount = count($ipfile); //set defaul increment to true $increment = true; //this is the persons ip address $ip = $_SERVER['REMOTE_ADDR']. $pageid; //start loop for($x=0;$x<=$linecount;$x++) { //does ip match an ip in ips.txt if(intval($ipfile[$x]. $pageid) == $ip) $increment = false; } //if it matches if ($increment == true) { $newhit = $hits + 1; mysql_query("UPDATE members SET counter='$newhit' WHERE pageid='$pageid'"); $filenewip = fopen("ips.txt",'a'); fwrite($filenewip,$ip."\n"); } echo $newhit; ?> Quote Link to comment https://forums.phpfreaks.com/topic/175149-solved-hit-counter-problem/ Share on other sites More sharing options...
mattal999 Posted September 22, 2009 Share Posted September 22, 2009 Echo this in the loop and see what values are being returned: echo intval($ipfile[$x]. $pageid) . " == " . $ip . "<br />"; Quote Link to comment https://forums.phpfreaks.com/topic/175149-solved-hit-counter-problem/#findComment-923102 Share on other sites More sharing options...
squiblo Posted September 22, 2009 Author Share Posted September 22, 2009 1 == 86.165.117.2321 this was returned, the "1" before "==" is the appended pageid Quote Link to comment https://forums.phpfreaks.com/topic/175149-solved-hit-counter-problem/#findComment-923103 Share on other sites More sharing options...
mattal999 Posted September 22, 2009 Share Posted September 22, 2009 Ok. So basically the $ipfile[$x] is returning nothing, correct? Try echoing this outside the loop: $ipfile[0] Quote Link to comment https://forums.phpfreaks.com/topic/175149-solved-hit-counter-problem/#findComment-923105 Share on other sites More sharing options...
mikesta707 Posted September 22, 2009 Share Posted September 22, 2009 what is $pageid? i never see it defined anywhere in that script. btw you want your for loop to have x < $linecount, because since arrays are zero based, once you hit the actual value of line count, (say its 5) you will get an array index that is out of bounds (or in PHP, it will be a null value, i believe) for example, say you have an array with a length of 5, it will look like 0 : value 1 : value 2 : value 3 : value 4 : value if you try to pass an index of 5 into that array, there will be no value for it Quote Link to comment https://forums.phpfreaks.com/topic/175149-solved-hit-counter-problem/#findComment-923107 Share on other sites More sharing options...
squiblo Posted September 22, 2009 Author Share Posted September 22, 2009 "$ipfile[0]" is echoing the first line on the .txt file, this is what the file looks like... 86.165.117.2321 86.165.117.2321 86.165.117.2321 this is what i echoed... (in the loop)... echo intval($ipfile[$x]. $pageid) . " == " . $ip . "<br />"; (out of the loop) echo $ipfile[0]; and this is what returned... 86 == 86.165.117.2321 86 == 86.165.117.2321 1 == 86.165.117.2321 86.165.117.2321 Quote Link to comment https://forums.phpfreaks.com/topic/175149-solved-hit-counter-problem/#findComment-923109 Share on other sites More sharing options...
mikesta707 Posted September 22, 2009 Share Posted September 22, 2009 thats because you are using intval its taking what it thinks is some crazy double and casting it as an int. don't use int val and try again Quote Link to comment https://forums.phpfreaks.com/topic/175149-solved-hit-counter-problem/#findComment-923112 Share on other sites More sharing options...
squiblo Posted September 22, 2009 Author Share Posted September 22, 2009 without intval this is returned... 86.165.117.2321 1 == 86.165.117.2321 Quote Link to comment https://forums.phpfreaks.com/topic/175149-solved-hit-counter-problem/#findComment-923119 Share on other sites More sharing options...
mikesta707 Posted September 22, 2009 Share Posted September 22, 2009 $ipfile[$x].$pageid try that? maybe post your code. for some reason you are concatenating a space onto the end of the ip address, etween the $pageid and the ip Quote Link to comment https://forums.phpfreaks.com/topic/175149-solved-hit-counter-problem/#findComment-923122 Share on other sites More sharing options...
squiblo Posted September 22, 2009 Author Share Posted September 22, 2009 <?php //connect to db mysql_connect("","","") or die ("Couldn't connect"); mysql_select_db("squiblo_com") or die ("Couldn't find db"); $query = mysql_query("SELECT * FROM members WHERE pageid='$pageid'"); $row = mysql_fetch_assoc($query); //get current hits $hits = $row['counter']; //get ip file $ipfile = file("ips.txt"); $linecount = count($ipfile); //set defaul increment to true $increment = true; //this is the persons ip address $ip = $_SERVER['REMOTE_ADDR']. $pageid; //start loop for($x=0;$x<=$linecount;$x++) { //does ip match an ip in ips.txt if(intval($ipfile[$x].$pageid) == $ip) $increment = false; echo ($ipfile[$x].$pageid) . " == " . $ip . "<br />"; } //if it matches if ($increment == true) { $newhit = $hits + 1; mysql_query("UPDATE members SET counter='$newhit' WHERE pageid='$pageid'"); $filenewip = fopen("ips.txt",'a'); fwrite($filenewip,$ip."\n"); } echo $ipfile[0]; ?> Quote Link to comment https://forums.phpfreaks.com/topic/175149-solved-hit-counter-problem/#findComment-923124 Share on other sites More sharing options...
mikesta707 Posted September 22, 2009 Share Posted September 22, 2009 try trim($ipfile[$x]).$pageid Quote Link to comment https://forums.phpfreaks.com/topic/175149-solved-hit-counter-problem/#findComment-923127 Share on other sites More sharing options...
squiblo Posted September 22, 2009 Author Share Posted September 22, 2009 on the if statement or the echo? Quote Link to comment https://forums.phpfreaks.com/topic/175149-solved-hit-counter-problem/#findComment-923130 Share on other sites More sharing options...
mikesta707 Posted September 22, 2009 Share Posted September 22, 2009 both Quote Link to comment https://forums.phpfreaks.com/topic/175149-solved-hit-counter-problem/#findComment-923142 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.