Jump to content

[SOLVED] IP Blocker txt file based


djtozz

Recommended Posts

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

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.

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,

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.