In rete si trovano già numerosi calcolatori di indirizzi IP e rete, come per esempio questo oppure questo.
Prendendo spunto da essi ecco una classe in PHP per ottenere i medesimi risultati. Lo scopo principale in questo caso è utilizzare le operazioni binarie in PHP per calcolare i vari valori di ID Rete, Subnet mask, ecc.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 |
<?php class IPCalc { const NO_ERROR = 0; const IP_ERROR = 1; const NETMASK_ERROR = 2; private $ip = array(0,0,0,0); private $netmask = array(0,0,0,0); private $wildcard = array(0,0,0,0); private $netid = array(0,0,0,0); private $broadcast = array(0,0,0,0); private $hostmin = array(0,0,0,0); private $hostmax = array(0,0,0,0); private $nhosts = 0; private $nnetmask = 24; private $class = ""; private $error = self::IP_ERROR; private $classes = array( //min max 1st octet network=1,hosts=0 "A" => array(1, 126, 0, array(1,0,0,0)), "A localhost" => array(127, 127, 0, array(1,0,0,0)), "B" => array(128, 191, 10, array(1,1,0,0)), "C" => array(192, 223, 110, array(1,1,1,0)), "D" => array(224, 239, 1110, array()), "E" => array(240, 254, 1111, array()) ); function setIp($ip) { if( $this->checkValidIp( $ip ) ) { $tmp = explode(".",$ip); foreach( $tmp as $k => $v ) $this->ip[$k] = ( $v ); $this->error = self::NO_ERROR; } } function getError() { return $this->error; } function checkValidIp( $ip ) { return (filter_var($ip, FILTER_VALIDATE_IP) !== false); } function setNetwork( $ip ) { $tmp = explode("/",$ip); $this->setIp($tmp[0]); $this->setNetMask(count($tmp)>1?$tmp[1]:24); $this->setWildcard(); $this->setNetId(); $this->setBroadcast(); $this->setHosts(); $this->setClass(); } function setNetMask( $mask = 24 ) { $this->nnetmask = (int)$mask; if( $mask < 1 || $mask > 32 ) $this->error = self::NETMASK_ERROR; for( $i = 0; $i < 32; $i++ ) { $this->netmask[floor($i/8)] += ( $i < $mask ? pow(2, 7-$i%8) : 0 ); } //foreach( $this->netmask as $k => $v ) $this->netmask[$k] = ($v); } function setWildcard() { foreach( $this->netmask as $k => $v ) $this->wildcard[$k] = 255 ^ $v ; } function setNetId() { foreach( $this->netmask as $k => $v ) $this->netid[$k] = $this->ip[$k] & $v ; } function setBroadcast() { foreach( $this->wildcard as $k => $v ) $this->broadcast[$k] = $v | $this->ip[$k] ; } function setHosts() { $this->hostmin = $this->netid; $this->hostmin[3]++; $this->hostmax = $this->broadcast; $this->hostmax[3]--; $this->nhosts = pow(2,32-$this->nnetmask) - 2; } function setClass() { foreach( $this->classes as $c => $vals ) { if( $this->ip[0] >= $vals[0] && $this->ip[0] <= $vals[1] ) $this->class = $c; } } function getBinary($val) { $tmp = array(); foreach( $val as $add ) { array_push( $tmp, str_pad( decbin($add) , 8, "0", STR_PAD_LEFT ) ); } return implode(".",$tmp); } function getDecimal($val) { $tmp = array(); foreach( $val as $add ) { array_push( $tmp, ($add) ); } return implode(".",$tmp); } function getBinaryIp() { return $this->getBinary( $this->ip ); } function getBinaryMask() { return $this->getBinary( $this->netmask ); } function getBinaryWildcard() { return $this->getBinary( $this->wildcard ); } function getBinaryNetID() { return $this->getBinary( $this->netid ); } function getBinaryBroadcast() { return $this->getBinary( $this->broadcast ); } function getBinaryHostMax() { return $this->getBinary( $this->hostmax ); } function getBinaryHostMin() { return $this->getBinary( $this->hostmin ); } function getIp() { return $this->getDecimal( $this->ip ); } function getMask() { return $this->getDecimal( $this->netmask ); } function getWildcard() { return $this->getDecimal( $this->wildcard ); } function getNetID() { return $this->getDecimal( $this->netid ); } function getBroadcast() { return $this->getDecimal( $this->broadcast ); } function getHostMax() { return $this->getDecimal( $this->hostmax ); } function getHostMin() { return $this->getDecimal( $this->hostmin ); } function getNHosts() { return $this->nhosts; } function getNNetMask() { return $this->nnetmask; } function getClass($pos) { if( $pos == -1 ) return $this->class; else return $this->classes[$this->class][$pos]; } } ?> |
Ecco di seguito un esempio di utilizzo:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
<?php $ip = new IPCalc(); $ip->setNetwork( "192.168.0.1/25" ); echo $ip->getBinaryIp(); echo "<br>"; echo $ip->getIp(); echo "<br>"; echo $ip->getBinaryMask(); echo "<br>"; echo $ip->getMask(); echo "<br>"; echo $ip->getBinaryWildcard(); echo "<br>"; echo $ip->getWildcard(); echo "<br>"; echo $ip->getBinaryNetID(); echo "<br>"; echo $ip->getNetID(); echo "<br>"; echo $ip->getBinaryBroadcast(); echo "<br>"; echo $ip->getBroadcast(); echo "<br>"; echo $ip->getBinaryHostMax(); echo "<br>"; echo $ip->getHostMax(); echo "<br>"; echo $ip->getBinaryHostMin(); echo "<br>"; echo $ip->getHostMin(); echo "<br>"; echo $ip->getNHosts(); ?> |