Skip to content

Instantly share code, notes, and snippets.

@bognerf
Created September 27, 2013 09:59
Show Gist options
  • Save bognerf/6726409 to your computer and use it in GitHub Desktop.
Save bognerf/6726409 to your computer and use it in GitHub Desktop.
Prüfung, ob IP-Adresse eines Users innerhalb der CIDR-Maske
<?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;
}
}
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