我似乎无法让它发挥作用。它获得了json和所有内容,它只是不想检查请求IP。
$wl_loc = "whitelist.json";
$wl = json_decode(file_get_contents($wl_loc), TRUE);
function get_client_ip_server() {
$ipaddress = '';
if ($_SERVER['HTTP_CLIENT_IP'])
$ipaddress = $_SERVER['HTTP_CLIENT_IP'];
else if($_SERVER['HTTP_X_FORWARDED_FOR'])
$ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
else if($_SERVER['HTTP_X_FORWARDED'])
$ipaddress = $_SERVER['HTTP_X_FORWARDED'];
else if($_SERVER['HTTP_FORWARDED_FOR'])
$ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
else if($_SERVER['HTTP_FORWARDED'])
$ipaddress = $_SERVER['HTTP_FORWARDED'];
else if($_SERVER['REMOTE_ADDR'])
$ipaddress = $_SERVER['REMOTE_ADDR'];
else
$ipaddress = 'UNKNOWN';
return $ipaddress;
}
$urip = get_client_ip_server();
foreach($wl as $arr) {
$ip = $arr['ip'];
if($urip == $ip) {
header("Location: /maintenance/");
}
}
和我的whitelist.json
[
{
"ip": "0.123.456.789"
},
{
"ip": "123.456.789.0"
}
]
我已经挣扎了好几个小时,看着整个互联网,我一无所获。我想要实现的是通过每个IP循环然后检查请求IP然后如果真的不重定向,否则如果为false然后重定向。
答案 0 :(得分:0)
尝试以下代码,
$newArray = array();
$myCallback = function($key) use(&$newArray){
$newArray[] = $key["ip"];
};
array_walk($wl, $myCallback);
$urip = get_client_ip_server();
if(!in_array($urip, $newArray)) {
header("Location: /maintenance/");
}
这里有一个简单的逻辑,
从主数组中创建一个新数组,该数组在单个维度中仅包含IP,并且使用if
语句完成其余的工作。