INET_ATON() and INET_NTOA() in PHP?

Posted by blerh on Stack Overflow See other posts from Stack Overflow or by blerh
Published on 2010-05-02T17:45:05Z Indexed on 2010/05/02 17:47 UTC
Read the original article Hit count: 269

Filed under:
|
|
|
|

I want to store IP addresses in my database, but I also need to use them throughout my application. I read about using INET_ATON() and INET_NTOA() in my MySQL queries to get a 32-bit unsigned integer out of an IP address, which is exactly what I want as it will make searching through the database faster than using char(15).

The thing is, I can't find a function that does the same sort of thing in PHP. The only thing I came across is:

http://php.net/manual/en/function.ip2long.php

So I tested it:

$ip = $_SERVER['REMOTE_ADDR'];
echo ip2long($ip);

And it outputs nothing. In the example they gave it seems to work, but then again I'm not exactly sure if ip2long() does the same thing as INET_ATON().

Does someone know a PHP function that will do this? Or even a completely new solution to storing an IP address in a database?

Thanks.

© Stack Overflow or respective owner

Related posts about php

Related posts about ip-address