note 36591 added to ref.gmp

From: Date: Wed, 15 Oct 2003 17:51:02 +0000
Subject: note 36591 added to ref.gmp
Groups: php.notes 
Request: Send a blank email to php-notes+get-58658@lists.php.net to get a copy of this message
============================================================= A set of very nice functions to handle IP Address with gmplib: The best way to store a range into a database is store: dNet ..... decimal representation of a Net dMask .... decimal representation of a Mask All another parameters can be calculated. <? /* f_ip2dec($a) ................... IP string to decimal f_dec2ip($a) ................... decimal to IP string f_dec2ipall($dNet,$dMask) ...... decimal Net and Mask to an Array with several IP parameters f_dec2cidr($a) ................. decimal Mask to CIDR f_and($a,$b) ................... and f_or($a,$b) .................... or f_xor($a,$b) ................... xor f_not($a) ...................... not f_dec2bin($a) .................. decimal to binary string f_bin2dec($a) .................. binary string to decimal */ function f_and($a,$b){ $a=gmp_init(strval($a)); $b=gmp_init(strval($b)); $d=gmp_and($a,$b); return floatval(gmp_strval($d)); } function f_or($a,$b){ $a=gmp_init(strval($a)); $b=gmp_init(strval($b)); $d=gmp_or($a,$b); return floatval(gmp_strval($d)); } function f_xor($a,$b){ $a=gmp_init(strval($a)); $b=gmp_init(strval($b)); $d=gmp_xor($a,$b); return floatval(gmp_strval($d)); } function f_not($a){ $a=gmp_init(strval($a)); $d=gmp_strval($a,2); $d=str_replace("1","x",$d); $d=str_replace("0","1",$d); $d=str_replace("x","0",$d); $d=gmp_init($d,2); return floatval(gmp_strval($d,10)); } function f_dec2bin($a){ $a=gmp_init(strval($a)); return gmp_strval($a,2); } function f_bin2dec($a){ $a=gmp_init(strval($a),2); return floatval(gmp_strval($a,10)); } function f_ip2dec($a){ $d = 0.0; $b = explode(".", $a,4); for ($i = 0; $i < 4; $i++) { $d *= 256.0; $d += $b[$i]; }; return $d; } function f_dec2ip($a){ $b=array(0,0,0,0); $c = 16777216.0; $a += 0.0; for ($i = 0; $i < 4; $i++) { $k = (int) ($a / $c); $a -= $c * $k; $b[$i]= $k; $c /=256.0; }; $d=join('.', $b); return($d); } function f_dec2cidr($a){ $a=gmp_init(strval($a)); $d=strlen(str_replace("0","",gmp_strval($a,2))); return $d; } function f_dec2ipall($dNet,$dMask){ $dWildCard=f_not($dMask); $IpAll["Net"]=f_dec2ip($dNet); $IpAll["Mask"]=f_dec2ip($dMask); $IpAll["WildCard"]=f_dec2ip($dWildCard); $IpAll["Cidr"]=f_dec2cidr($dMask); $IpAll["Bcast"]=f_dec2ip(f_or($dNet,$dWildCard)); $IpAll["nIp"]=$dWildCard+1; $IpAll["nIpUtil"]=$dWildCard-1; if($IpAll["nIp"] > 2){ $IpAll["IpFrom"]=f_dec2ip($dNet+1); $IpAll["IpTo"]=f_dec2ip($dNet+$dWildCard-1); } else { $IpAll["IpFrom"]="-"; $IpAll["IpTo"]="-"; $IpAll["nIpUtil"]=0; } return $IpAll; } ?> ============================================================= GMP install steps in Mandrake 9.1: ---------------------------------------------------------- cp -r /usr/src/php-devel/extensions/gmp /tmp/gmp cd /tmp/gmp phpize ./configure make install echo "extension = gmp.so" > /etc/php/90_gmp.ini Restart apache web server. ---------------------------------------------------------- gmp.so is in: /usr/lib/php/extensions/ look in phpinfo, the string: /etc/php/90_gmp.ini Needs these tools: autoconf automake libtool m4 php430-devel-430-11mdk.rpm all rpm´s that are envolved to run and compile gmp (*gmp*.rpm) Some docs about self contained extensions: /usr/share/doc/php430-devel-430/SELF-CONTAINED-EXTENSIONS ============================================================= ---- Manual Page -- http://www.php.net/manual/en/ref.gmp.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+36591 Delete -- http://master.php.net/manage/user-notes.php?action=delete+36591&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+36591&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#58658) next »