Mailinglisten-Archive |
Hab mal fix was zusammengehackt - besonders die 2. Funktion :)
Sollte die noch auf einen Rest bei $DEC ueberpruefen und ggf. -1
zurueckliefern?
Balu
----- 8< -----
<?php
// This function returns the 32bit-decimal-value for a given IP-address
// Thanx to Michael Knetsch for his tip
function IPtoDEC($IP){
$IPsplit=explode(".", $IP);
if (sizeof($IPsplit)!=4) return -1;
$result=0;
for ($i=0; $i<4; $i++){
if ($IPsplit[$i]<0 || $IPsplit[$i]>255) return -1;
$result+=$IPsplit[$i]*pow(256, 3-$i);
}
return $result;
}
// This function returns the IP-address for a given 32bit-decimal-value
// (working quick-hack ;) If you know a better solution, mail me
function DECtoIP($DEC){
$result="";
for ($i=0; $i<4; $i++){
$power=pow(256, 3-$i);
$part=floor($DEC/ $power);
$DEC=$DEC - $part * $power;
$result=$result.".".$part;
}
return substr($result, 1);
}
// Example:
$IP="212.172.185.1";
echo "IP: ".$IP."<br>";
$DEC=IPtoDEC($IP);
echo "DEC: ".$DEC."<br>";
$IP=DECtoIP($DEC);
echo "IP: ".$IP."<br>";
?>
php::bar PHP Wiki - Listenarchive