Keverw
Keverw

Reputation: 3786

inet_pton does not work on local ip addresses like 10.0.1.4

<?php
//$ipaddress = $_SERVER['REMOTE_ADDR'];
$ipaddress = '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?

Upvotes: 0

Views: 2151

Answers (2)

Emil Vikstr&#246;m
Emil Vikstr&#246;m

Reputation: 91942

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?

Upvotes: 1

Neigyl R. Noval
Neigyl R. Noval

Reputation: 6038

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.

Upvotes: 0

Related Questions