当前位置: 移动技术网 > IT编程>开发语言>PHP > PHP禁止个别IP访问网站

PHP禁止个别IP访问网站

2019年04月03日  | 移动技术网IT编程  | 我要评论

想不让某个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;
}

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网