Example #1
0
/**
 * Checks if client IP address is in the whitelist
 * Useful to create simple IP access rules
 *
 * @param $whitelist array of IPv4 addresses
 * @return true if client IP address is in the $allowed list
 */
function allowed_ip($whitelist)
{
    if (php_sapi_name() == 'cli') {
        return true;
    }
    $ip = IPv4_to_GeoIP(client_ip());
    return match_ip($ip, $whitelist);
}
Example #2
0
 public function test2()
 {
     $allowed = array('192.168.0.1', '80.0.0.0/8', '240.0.0.0/8', '230.230.0.0/16', '220.220.220.0/24');
     $this->assertEquals(match_ip('220.220.220.42', $allowed), true);
     $this->assertEquals(match_ip('230.230.11.42', $allowed), true);
     $this->assertEquals(match_ip('240.212.11.42', $allowed), true);
     $this->assertEquals(match_ip('230.212.11.42', $allowed), false);
     $this->assertEquals(match_ip('220.220.11.42', $allowed), false);
     $this->assertEquals(match_ip('241.212.11.42', $allowed), false);
     $this->assertEquals(match_ip('240.212.11.42.1', $allowed), false);
     $this->assertEquals(match_ip('111.111.111.111/8', $allowed), false);
     $this->assertEquals(match_ip('300.111.111.111', $allowed), false);
 }