Jump to content

Difficulties with simple IP ban reader from file


sphinx

Recommended Posts

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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"

Link to comment
Share on other sites

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.';
    ?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.