penisland Posted November 17, 2013 Share Posted November 17, 2013 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 Link to comment https://forums.phpfreaks.com/topic/283993-is-equal-to-not-working-for-certain-varaiable/ 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. Link to comment https://forums.phpfreaks.com/topic/283993-is-equal-to-not-working-for-certain-varaiable/#findComment-1458664 Share on other sites More sharing options...
Barand Posted November 17, 2013 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))) Link to comment https://forums.phpfreaks.com/topic/283993-is-equal-to-not-working-for-certain-varaiable/#findComment-1458665 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.