Created
September 27, 2013 09:59
-
-
Save bognerf/6726409 to your computer and use it in GitHub Desktop.
Prüfung, ob IP-Adresse eines Users innerhalb der CIDR-Maske
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Default_Model_Cidr | |
{ | |
public static function match($ip, $range) | |
{ | |
list ($subnet, $bits) = explode('/', $range); | |
$ip = ip2long($ip); | |
$subnet = ip2long($subnet); | |
$mask = -1 << (32 - $bits); | |
$subnet &= $mask; | |
$r = ($ip & $mask) == $subnet; | |
if(!$r) | |
{ | |
throw new Zend_Exception('Nicht innerhalb des Adressraums', 401); | |
} | |
return $r; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
try | |
{ | |
Default_Model_Cidr::match($_SERVER['REMOTE_ADDR'], "192.168.71.0/24"); | |
} catch (Zend_Exception $e) | |
{ | |
$this->redirect('/ausleihe/index/index'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment