0

I'm trying to compare some IP addresses that are stored as strings. I read about ip2long() to convert the string to an integer so I'm playing around with it to see if it would do what I want. So, initially, I'm just trying to see what the converted IP looks like. I wrote the following code to look at that but nothing is being converted. I don't see what I'm doing wrong. Here is the code:

<?php
foreach($form->data[vicondb] as $video_net_key) 
{
    if ($video_net_key['ipaddress'] != null)
    {
        echo "The IP address is " . $video_net_key['ipaddress'];
        $iplong = ip2long($video_net_key['ipaddress']);
        echo " --- The ip long conversion is " . $iplong . "<br>";
    }

} // End foreach($form->data[vicondb] as $video_net_key)

echo "End of both foreach statements<br>";
?>

Here is a sample of the result:

The IP address is 172.26.0.10 --- The ip long conversion is

The IP address is 172.26.0.31 --- The ip long conversion is

The IP address is 172.26.0.32 --- The ip long conversion is

The IP address is 172.26.0.33 --- The ip long conversion is

Here is a sample of the var_dump() for IP and converted to integer:

string(12) "172.26.0.10 "
bool(false)
string(12) "172.26.0.31 "
bool(false)
string(12) "172.26.0.32 "
bool(false) 
Community
  • 1
  • 1
ChessDad
  • 45
  • 6
  • `var_dump($video_net_key['ipaddress']); var_dump($iplong);` – AbraCadaver Feb 18 '16 at 16:12
  • Thank you for your comment. I tried your code. It shows the ip address for the first line and shows 0 for all the iplong variables. However, when I put in an IP address in an online converter I get 2887385121 for the IP 172.26.0.33 but it comes up 0 in my script. – ChessDad Feb 18 '16 at 17:08
  • And the var_dump on the IP shows length 11? – AbraCadaver Feb 18 '16 at 17:13
  • Here is a sample of the dump: string(12) "172.26.0.10 " bool(false) string(12) "172.26.0.31 " bool(false) string(12) "172.26.0.32 " bool(false) – ChessDad Feb 18 '16 at 17:28

2 Answers2

0

It could be something similar like this

"Because PHP's integer type is signed, and many IP addresses will result in negative integers on 32-bit architectures, you need to use the "%u" formatter of sprintf() or printf() to get the string representation of the unsigned IP address." From ip2long php.net article

This should work:

echo " --- The ip long conversion is " . sprintf("%u", ip2long($video_net_key['ipaddress'])) . "<br>";
Tom Udding
  • 2,264
  • 3
  • 20
  • 30
  • Thanks for the reply. I tried your code and get the following: The IP address is 172.26.0.10 --- The ip long conversion is 0 The IP address is 172.26.0.31 --- The ip long conversion is 0 The IP address is 172.26.0.32 --- The ip long conversion is 0 When I put in an IP address in an online converter I get 2887385121 for the IP 172.26.0.33 – ChessDad Feb 18 '16 at 16:58
  • @ChessDad I don't know any other explanation for this problem. – Tom Udding Feb 18 '16 at 17:45
0

You need to trim the IP as it seems to have a trailing space and is not valid:

$iplong = ip2long(trim($video_net_key['ipaddress']));
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87