ip2long(),long2ip() functions in php:

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″

?>

Leave a Reply

You must be logged in to post a comment.


All material @ copyrighted by chrisranjana.com. If you want to link to this article you are welcome to do so. Unauthorized publication is strictly prohibited. This developer tutorial website contains articles by Php programmers , Software developers, Mysql programmers and asp c# programmers. This website also contains ajax tutorials and advanced mysql sql stored procedures and functions tutorials and sample codes.