PHP禁止个别IP访问网站
想不让某个ip访问网站,可以封他的ip,下面就提供这个方法。看下面的代码。
function get_ip_data(){
$ip=file_get_contents("http://ip.taobao.com/service/getipinfo.php?ip=".get_client_ip());
$ip = json_decode($ip);
if($ip->code){
return false;
}
$data = (array) $ip->data;
if($data['region']=='湖北省' && !iscrawler()){
exit('http://www.a.net');
}
}
function iscrawler() {
$spidersite= array(
"tencenttraveler",
"baiduspider+",
"baidugame",
"googlebot",
"msnbot",
"sosospider+",
"sogou web spider",
"ia_archiver",
"yahoo! slurp",
"youdaobot",
"yahoo slurp",
"msnbot",
"java (often spam bot)",
"baiduspider",
"voila",
"yandex bot",
"bspider",
"twiceler",
"sogou spider",
"speedy spider",
"google adsense",
"heritrix",
"python-urllib",
"alexa (ia archiver)",
"ask",
"exabot",
"custo",
"outfoxbot/yodaobot",
"yacy",
"surveybot",
"legs",
"lwp-trivial",
"nutch",
"stackrambler",
"the web archive (ia archiver)",
"perl tool",
"mj12bot",
"netcraft",
"msiecrawler",
"wget tools",
"larbin",
"fish search",
);
if(in_array(strtolower($_server['http_user_agent']),$spidersite)){
return true;
}else{
return false;
}
}
//取客户端 ip
function get_client_ip()
{
if (isset($_server)){
if (isset($_server["http_x_forwarded_for"])){
$realip = $_server["http_x_forwarded_for"];
} else if (isset($_server["http_client_ip"])) {
$realip = $_server["http_client_ip"];
} else {
$realip = $_server["remote_addr"];
}
} else {
if (getenv("http_x_forwarded_for")){
$realip = getenv("http_x_forwarded_for");
} else if (getenv("http_client_ip")) {
$realip = getenv("http_client_ip");
} else {
$realip = getenv("remote_addr");
}
}
return $realip;
}
上一篇: 从一道题看js的拆箱操作
下一篇: 产品营销推广 决定一个产品未来生死