Re: Restrict Access per IP Addresses

From: Date: Thu, 01 Mar 2001 00:55:34 +0000
Subject: Re: Restrict Access per IP Addresses
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-42104@lists.php.net to get a copy of this message
At 00:58 01.03.2001, Nazoréen said: --------------------[snip]-------------------- >well i have a problem, i try make an authentication per range IP Address >like : >from 194.195.196.0 to 194.195.196.255 but it doesn't work, except if i make >an authentication for only one IP Address. I tried to make with "LOOP" and >"WHILE" tag, but i got the problem everytime ! > >This is the last example script i made with two domains which accept only >the range IP Address for each : > ><?php > >$host = getenv("REMOTE_ADDR"); >$ip = range(0,255); >if ($host != "194.195.196.($ip)) >if ($host != "195.196.197.($ip)) --------------------[snip]-------------------- Salut Anthony, you may check if an address is within a certain network/subnet, you need to compare each octet. For example: Address 192.168.11.212 Network 192.168.12.255 Mask 255.255.255.0 192 & 255 = 192 192 & 255 = 192 OK 168 & 255 = 168 168 & 255 = 168 OK 11 & 255 = 11 12 & 255 = 12 NOT WITHIN SUBNET I've put this together in this littel test form: --------------------[snip]-------------------- <?php function contains_ip($network, $snmask, $address) { if (!is_array($network)) $network = explode(".", $network); if (!is_array($snmask)) $snmask = explode(".", $snmask); if (!is_array($address)) $address = explode(".", $address); for ($i=0; $i<4; ++$i) { $a = $address[$i]*1; $m = $snmask[$i]*1; $n = $network[$i]*1; $m1 = $a & $m; $m2 = $n & $m; if (($a & $m) != ($n & $m)) return false; } return true; } if ($network && $subnet && $address) $result = contains_ip($network, $subnet, $address); ?> <html> <body> <?php if (isset($result)):?> <h4><?=$address?> is<?php if(!$result):?> <i>not</i><?php endif;?> contained in <?=$network?>, mask <?=$subnet?></h4> <?php endif;?> <form> Enter Address: <input type="text" name="address" value="<?=$address?>"><br> Enter Network: <input type="text" name="network" value="<?=$network?>"><br> Enter Subnetmask: <input type="text" name="subnet" value="<?=$subnet?>"><br> <input type="submit"> </form> </body> </html> --------------------[snip]-------------------- ...ebird >O Ernest E. Vogelsinger (\) http://www.1-at-web.at/ ^ ICQ# 13394035

« previous php.general (#42104) next »