管理员访问的白名单IP地址

时间:2011-09-19 00:07:21

标签: php

我的网站上有一个区域,我只想访问一些人。我的代码现在只适用于一个IP地址,但我希望能够添加更多。

以下是我正在使用的内容:

$ipaddress = $_SERVER['REMOTE_ADDR'];
if($ipaddress == '111.111.111.111') {
//Action for allowed IP Addresses
} else {
//Action for all other IP Addresses
echo 'You are not authorized here.'; 
echo "<br />IP Address: ".$_SERVER['REMOTE_ADDR'];
exit;
}

1 个答案:

答案 0 :(得分:11)

$whitelist = array('111.111.111.111', '111.111.111.112');
if (in_array($_SERVER['REMOTE_ADDR'], $whitelist)) {
    //Action for allowed IP Addresses
} else {
    //Action for all other IP Addresses
    echo 'You are not authorized here.'; 
    echo "<br />IP Address: ".$_SERVER['REMOTE_ADDR'];
    exit;
}