RE: [PHP] MySQl IP Address Storage

From: Date: Thu, 20 Sep 2001 18:59:11 +0000
Subject: RE: [PHP] MySQl IP Address Storage
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-68045@lists.php.net to get a copy of this message
On 20-Sep-2001 Felix wrote: > Hello all > > Can anyone suggest their best idea for a way to store IP addresses in a > MySQL Database? > INT UNSIGNED and use the MySQL functions INET_ATON() & INET_NTOA() for converting. If you're on a pre-3.23 MySQL server then: // Convert IP string (XXX.YYY.WWW.ZZZ) to float and back function inet_aton($a) { $inet = 0.0; // $a=preg_replace('/^[0-9a-f:]*:/', '', $a); // eat IPv6 off the front $t = explode('.', $a); while (list(,$i) = each($t)) { $inet *= 256.0; $inet += $i; }; return $inet; } function inet_ntoa($n) { $t=array(0,0,0,0); $msk = 16777216.0; $n += 0.0; if ($n < 1) return('&nbsp;'); for ($i = 0; $i < 4; $i++) { $k = (int) ($n / $msk); $n -= $msk * $k; $t[$i]= $k; $msk /=256.0; }; $a=join('.', $t); return($a); } Regards, -- Don Read dread@texas.net -- It's always darkest before the dawn. So if you are going to steal the neighbor's newspaper, that's the time to do it.

« previous php.general (#68045) next »