djtozz Posted July 6, 2009 Share Posted July 6, 2009 Hello, I'm using following code to block certain IP adresses <?php $deny_ips = file('ip.txt'); $ip = isset($_SERVER['REMOTE_ADDR']) ? trim($_SERVER['REMOTE_ADDR']) : ''; if (($i = array_search($ip, $deny_ips)) !== FALSE){ print "Your IP address ('$ip') was blocked "; exit; } ?> ip.txt file: 94.210.80.40 83.87.15.160 173.45.79.217 128.230.204.198 114.40.161.39 Problem: The script only blocks the last IP adress from ip.txt Any idea how I can fixs this Thanks in advance Kind regards, Nico Link to comment https://forums.phpfreaks.com/topic/164924-solved-ip-blocker-txt-file-based/ Share on other sites More sharing options...
PFMaBiSmAd Posted July 6, 2009 Share Posted July 6, 2009 The file() function - Note: Each line in the resulting array will include the line ending, unless FILE_IGNORE_NEW_LINES is used, so you still need to use rtrim() if you do not want the line ending present. The new line character(s) are included by default and a comparison or search won't match. Use FILE_IGNORE_NEW_LINES as the second parameter in the file() function call. If you just want to find if a value is in an array, use the in_array function. Link to comment https://forums.phpfreaks.com/topic/164924-solved-ip-blocker-txt-file-based/#findComment-869670 Share on other sites More sharing options...
djtozz Posted July 6, 2009 Author Share Posted July 6, 2009 Use FILE_IGNORE_NEW_LINES as the second parameter in the file() function call. If you just want to find if a value is in an array, use the in_array function. Thanks for the help, I understand the problem but I'm not sure how to add the FILE_IGNORE_NEW_LINES parameter (sorry, my programming level is probably less then n00b) Can somebody include this in my code above please? Kind regards, Link to comment https://forums.phpfreaks.com/topic/164924-solved-ip-blocker-txt-file-based/#findComment-869672 Share on other sites More sharing options...
shergold Posted July 6, 2009 Share Posted July 6, 2009 sorry if im wrong but i believe he is saying: $deny_ips = file('ip.txt',FILE_IGNORE_NEW_LINES); Shergold. Link to comment https://forums.phpfreaks.com/topic/164924-solved-ip-blocker-txt-file-based/#findComment-869674 Share on other sites More sharing options...
djtozz Posted July 6, 2009 Author Share Posted July 6, 2009 sorry if im wrong..... You're not! works like a charm! Thank you guys, apriciated! regards, Link to comment https://forums.phpfreaks.com/topic/164924-solved-ip-blocker-txt-file-based/#findComment-869754 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.