sphinx Posted August 7, 2012 Share Posted August 7, 2012 Hello, For some reason, it is still allowing blocked IP's onto my website, I want it to read the IP addresses from a .txt file. <?php $deny_ips = file('ip.txt'); $ip = isset($_SERVER['REMOTE_ADDR']) ? trim($_SERVER['REMOTE_ADDR']) : ''; if (($i = array_search($ip, $deny_ips)) !== FALSE){ print "The IP: ('$ip') has been blocked."; exit; } ?> <?php echo $_SERVER['REMOTE_ADDR']; ?> Accepted.</p> ip.txt $deny_ips = array( '209.240.206.199', '209.240.206.200', '209.240.206.201', '209.240.206.202',); Still allows it all through. Cheers Quote Link to comment Share on other sites More sharing options...
Jessica Posted August 7, 2012 Share Posted August 7, 2012 if (($i = array_search($ip, $deny_ips)) !== FALSE){ array_search returns a key or false. You then assign it to $i - the ACT of assigning it to $i will ALWAYS be true. So it will NEVER !== FALSE. Quote Link to comment Share on other sites More sharing options...
Christian F. Posted August 7, 2012 Share Posted August 7, 2012 Why store the results of array_search () in a variable, and why enclose it all in parentheses? Since you're not using the result of the search later on, I'd just compare it directly. if (array_search ($ip, $deny_ips) !== false) { } Although, that's not your problem in this case. I suspect that what you've posted in the second code block is the actual content of the file "ip.txt", in which case you really need to read up on what file () does in the PHP manual. If it's not, then you should look anyway, and pay really close attention to the data returned by the function. The devil's in the details, after all. Edit: Actually, Jesirose, I'm afraid you're wrong about that one. The act of assigning a value returns the value itself. So if you're storing false to a variable, and compare it to false in the same operation, the result of the test will be true. As evidenced here: php > if (($a = false) !== false) { echo 'Not true'; } php > if (($a = true) !== false) { echo 'True'; } True Quote Link to comment Share on other sites More sharing options...
sphinx Posted August 7, 2012 Author Share Posted August 7, 2012 Funny you should say that. I was actually looking at the script, I had the ip addresses wrong in the text file, if I have a list such as: 192.168.1.2 192.168.1.3 192.168.1.4 192.168.1.5 192.168.1.6 The only IP that it would ban is the last listed one. "192.168.1.6" Quote Link to comment Share on other sites More sharing options...
sphinx Posted August 7, 2012 Author Share Posted August 7, 2012 Cheers, this does the job fine: <?php $ips_file_path = 'ip.txt'; $my_ip = $_SERVER['REMOTE_ADDR']; $ips_list = file($ips_file_path); foreach (array_values($ips_list) AS $ip){ if (trim($ip) == $my_ip){ print "Banned mate."; exit; } } echo 'Hello.'; ?> Quote Link to comment Share on other sites More sharing options...
Christian F. Posted August 7, 2012 Share Posted August 7, 2012 I recommend running a var_dump () on the $deny_ips array, you should be able to spot the problem then. PS: Remember to either use <pre> tags, or look at the source of the page, to preserve whitespaces. Edit: Ah, I see you were able to solve it while I was typing this reply. Anyway, for posterity's sake I can inform you that the problem was because file () preserves the newlines when splitting content into an array, so all but the last IPs were actually looking like this "192.168.0.1\n". Which doesn't match what you were searching for. Quote Link to comment Share on other sites More sharing options...
DavidAM Posted August 7, 2012 Share Posted August 7, 2012 Anyway, for posterity's sake I can inform you that the problem was because file () preserves the newlines when splitting content into an array, so all but the last IPs were actually looking like this "192.168.0.1\n". Which doesn't match what you were searching for. So instead of using a loop, you could change that one line of code to: $deny_ips = array_map('trim', file('ip.txt')); to solve the problem. Using array_search() is a better solution than a loop. Yes, the function (probably) uses a loop; but that loop is coded in C and compiled, and will run faster than the loop in PHP. This is not a big deal with a short list like you have, but it is a good thing to keep in mind for those times when you are processing array data. 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.