开发者

inet_pton does not work on local ip addresses like 10.0.1.4

<?php
//$ipaddress = $_SERVER['REMOTE_ADDR'];
$ipaddr开发者_Python百科ess = '10.0.1.4';
$binip = inet_pton($ipaddress);
echo $binip;
?>

It returns blank. If i use my public facing IP it returns the right result. It will return a result for 192.168.1.2(My old LinkSys router had a ip similar to this). I'm testing my app on a Linux machine i have with a local ip. All Machines that connect to it uses a internal ip 10.0.1.XX. My app uses MySQL and if the binip is blank, it gives me a error. So i am not sure of a work around for local ip. I was thinking maybe it has to do with detecting if its a local ip in that format and then edit the ipaddress variable so its valid for inet_pton some how. Any ideas?


It works for me. I get a binary structure from your example code (although, there's no printable characters in it so if you run it in a web browser you'll probably get nothing).

Actually, I even tried this code which outputs the original address and shows that there's no information loss going on:

<?php
$ipaddress = '10.0.1.4';
$binip = inet_pton($ipaddress);
echo inet_ntop($binip);
?>

Maybe you are more interested in ip2long which converts the address to an integer? Or maybe your problem is elsewhere, for example in escaping the data before putting it into a database?


You have similar issues here:

http://php.net/manual/en/function.inet-pton.php

You can find out solutions from there too. Check out how that function returns FALSE.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜