PHP网站后台新增地区登录限制代码
function getIPs(){
global _C;
if(empty(_C['client_ip'])) {
if(getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown')) {
client_ip = getenv('HTTP_CLIENT_IP');
} elseif(getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown')) {client_ip = getenv('HTTP_X_FORWARDED_FOR');
} elseif(getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown')) {
client_ip = getenv('REMOTE_ADDR');
} elseif(isset(_SERVER['REMOTE_ADDR']) && _SERVER['REMOTE_ADDR'] && strcasecmp(_SERVER['REMOTE_ADDR'], 'unknown')) {
client_ip =_SERVER['REMOTE_ADDR'];
}
_C['client_ip'] =client_ip ? client_ip : 'unknown';
}
return_C['client_ip'];
}
//查询IP所在城市
function chaIPs(){
ip = getIPs();host = "https://cz88geoaliyun.cz88.net/search/ip/geo";
method = "POST";appcode = "0169cc17ba7d4f44b1683bf0a7774d88";
headers = array();
array_push(headers, "Authorization:APPCODE " . appcode);
//根据API的要求,定义相对应的Content-Type
array_push(headers, "Content-Type".":"."application/json; charset=UTF-8");
querys = "ip={ip}";
bodys = "null";url = host . "?" .querys;
curl = curl_init();
curl_setopt(curl, CURLOPT_CUSTOMREQUEST, method);
curl_setopt(curl, CURLOPT_URL, url);
curl_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_setopt(curl, CURLOPT_FAILONERROR, false);
curl_setopt(curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt(curl, CURLOPT_HEADER, true);
if (1 == strpos("".host, "https://"))
{
curl_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt(curl, CURLOPT_SSL_VERIFYHOST, false);
}
curl_setopt(curl, CURLOPT_POSTFIELDS,bodys);
ret = curl_exec(curl);
curl_close ( curl );
//截取{}中的字符串ret = substr(ret, strpos(ret, '{"code"'),strlen(ret));
//转化为json数据ret = json_decode(ret);
returnret->data->city;
}
//判断登录IP所在城市是否相符
header("Content-type: text/html; charset=utf-8");
cs = chaIPs();yzcs = "/(淄博)/i";//这里设置IP所在的城市
if (!preg_match(yzcs,cs)){
header('location: /404.html');
}