note 25373 added to function.gethostbyaddr

From: Date: Fri, 20 Sep 2002 19:59:52 +0000
Subject: note 25373 added to function.gethostbyaddr
Groups: php.notes 
Request: Send a blank email to php-notes+get-37068@lists.php.net to get a copy of this message
If you need to store an IP addresses in a database, you can convert and store it in an INT type column (4 bytes). The functions below can convert IP addresses to its integer decimal value and vice-versa. function ip2dec($ipaddr) { $base=explode(".",$ipaddr); $decimal=(double) $base[0]*16777216; $decimal+=$base[1]*65536; $decimal+=$base[2]*256; $decimal+=$base[3]; if($decimal>2147483647) { $decimal-=4294967296; } return (int) $decimal; } function dec2ip($dec) { if($dec<0) { $dec=(double) 4294967296+$dec; } if($dec>16777215) { $ip=$dec-(intval($dec/256)*256); $dec=(double) intval($dec/256); } else $ip="0"; if($dec>65535) { $ip=($dec-(intval($dec/256)*256)).".".$ip; $dec=(double) intval($dec/256); } else $ip="0.".$ip; if($dec>255) { $ip=($dec-(intval($dec/256)*256)).".".$ip; $dec=(double) intval($dec/256); } else $ip="0.".$ip; $ip=$dec.".".$ip; return (string) $ip; } -- http://www.php.net/manual/en/function.gethostbyaddr.php http://master.php.net/manage/user-notes.php?action=edit+25373 http://master.php.net/manage/user-notes.php?action=delete+25373 http://master.php.net/manage/user-notes.php?action=reject+25373

« previous php.notes (#37068) next »