-4

I have ips.txt file, there is : 31.146.153.182

ENDFILE-> this was my ip

and I have index.php

<?php 
    $file ='ips.txt';
    $ips = file($file);
    $client =  $_SERVER['REMOTE_ADDR'];
    if ($ips[0]==$client)  { 
        echo  "ip is blocked";
    } else {
       echo "it does not work";
    }
?>

And this script does not equaling $ips[0] == $client, but in echo it's actually the same.

Henders
  • 1,195
  • 1
  • 21
  • 27
James Kond
  • 11
  • 4

1 Answers1

0

By default, file copies along newlines, as documented. But the newline is not in the REMOTE_ADDR result.

You can avoid this behavior by adding the FILE_IGNORE_NEW_LINES flag to the file function, like this:

$ips = file($file, FILE_IGNORE_NEW_LINES);

Or you can alternatively trim the lines in your check:

if (trim($ips[0]) == $client) { ... }
Oldskool
  • 34,211
  • 7
  • 53
  • 66