ip2long(),long2ip() functions in php:
We can use ip2long() and long2ip() to store IP addresses as integers instead of strings in a database. This will reduce the storage space by almost a factor of four (15 bytes for char(15) vs. 4 bytes for the integer).
ip2long() :
int ip2long ( string ip_address)
int ip2long ( string ip_address)This function converts a string containing an (IPv4) Internet Protocol dotted address into a proper address.
Example :
<?php
$ip = gethostbyname(”www.cat45.com”);
$a = “The following URLs are equivalent:<br>\n”;
$a .= “http://www.cat45.com/, http://”.$ip.”/, and http://”.sprintf(”%u”,ip2long($ip)).”/<br>\n”;
echo $a;
?>
long2ip() :
string long2ip ( int proper_address)
string long2ip ( int proper_address)The function long2ip() generates an Internet address in dotted format (i.e.: aaa.bbb.ccc.ddd) from the proper address representation.
Example :
<?php
// make sure IPs are valid. also converts a non-complete IP into
// a proper dotted quad as explained below.
echo $ip = long2ip(ip2long(”127.0.0.1″)); // “127.0.0.1″
echo $ip = long2ip(ip2long(”10.0.0″)); // “10.0.0.0″
echo $ip = long2ip(ip2long(”10.0.256″)); // “10.0.1.0″
?>
