2

I've been fighting for hours ...

i want to understand if my ip is included in a list of ip ranges.

The list comes from a file, I do a foreach and an if

if(ip2long($_SERVER['REMOTE_ADDR']) >= ip2long($start) && ip2long($_SERVER['REMOTE_ADDR']) <= ip2long($end)){
    echo 'found';
}

...and it doesn't work...

I think the reason is this, if in my foreach I add this:

echo '<br>my '.$_SERVER['REMOTE_ADDR'].' - start '.$start.' - end '.$end.' --- my long '.ip2long($_SERVER['REMOTE_ADDR']).' - start long '.ip2long($start).' - end long '.ip2long($end);

The result I get looks like this:

my 146.241.156.220 - start 146.241.0.0 - end 146.241.255.255 --- my long 2465307868 - start long 2465267712 - end long
my 146.241.156.220 - start 1.6.0.0 - end 1.7.255.255 --- my long 2465307868 - start long 17170432 - end long
my 146.241.156.220 - start 1.22.0.0 - end 1.23.255.255 --- my long 2465307868 - start long 18219008 - end long
my 146.241.156.220 - start 1.38.0.0 - end 1.39.255.255 --- my long 2465307868 - start long 19267584 - end long
my 146.241.156.220 - start 1.186.0.0 - end 1.187.255.255 --- my long 2465307868 - start long 28966912 - end long 

Where did "end long" go? ip2long($end)

$end exists, works, but ip2long($end) is empty...

Why? What? Where? Who?

I've been fighting with this for hours, and something just isn't working, can you help me get out of this mess?

x43
  • 186
  • 2
  • 19
alebal
  • 5,838
  • 3
  • 10
  • 28
  • 2
    Check for any leading / trailing whitespace in `$end`. If `ip2long(trim($end))` works, then you know that's the problem ~ https://3v4l.org/YVuuh – Phil Jan 26 '21 at 23:13
  • I LOVE YOU! hours and hours lost... and a trim was enough... I will put it everywhere from now on... – alebal Jan 26 '21 at 23:19
  • This also illustrates a problem with using HTML output for debugging. You won't see any extra whitespace – Phil Jan 26 '21 at 23:20

0 Answers0