penisland Posted November 17, 2013 Share Posted November 17, 2013 (edited) I have a .txt document named bannedIPS.txt that has this in it: 12.326.25.3 192.168.1.1 207.236.2.32 10.218.24.5 and then my php script has this //Gets user's IP $ipaddress = $_SERVER['REMOTE_ADDR']; //List of IPs to block $fh = fopen('UserIP/bannedIPS.txt','a+'); $ipBlockList = array(); $countWhileLoop = 0; while($line = fgets($fh)) { $ipBlockList[$countWhileLoop] = $line; $countWhileLoop++; } fclose($fh); //Determine if user IP is on the list for($i=0; $i<=$countWhileLoop-1; $i++) { //Debug for seeing what $ipBlockList[$i] is echo $ipBlockList[$i]."<br/>"; if($ipaddress==$ipBlockList[$i]) { echo "banned"; exit; } } My IP address is 207.236.2.32 so it should display: 12.326.25.3192.168.1.1207.236.2.32 banned but it displays: 12.326.25.3192.168.1.1207.236.2.3210.218.24.5 so, the if statement isn't getting triggered. Why is that? I'v tried: if($ipaddress=="207.236.2.32") { echo "banned"; exit; } and then it works, but if it's the variable, it doesn't. PS. I just started learning PHP today from W3Schools and Google. Edit: Fixed typo Edited November 17, 2013 by penisland Quote Link to comment Share on other sites More sharing options...
digibucc Posted November 17, 2013 Share Posted November 17, 2013 i am not seeing a problem with your expected outcome. i'd do it a little different but that's normal, long story short i copy/pasted/uploaded and added my ip to bannedips.txt, it it prints: 12.326.25.3 192.168.1.1 207.236.2.32 10.218.24.5 xx.xx.xx.xx (my ip)banned sooo... yeah. Quote Link to comment Share on other sites More sharing options...
Solution Barand Posted November 17, 2013 Solution Share Posted November 17, 2013 Reading ends when length - 1 bytes have been read, on a newline (which is included in the return value) so you need to trim the lines from the file while($line = trim(fgets($fh))) 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.