欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

PHP禁止个别IP访问网站

程序员文章站 2022-06-20 08:32:29
想不让某个ip访问网站,可以封他的ip,下面就提供这个方法。看下面的代码。复制代码 代码如下:function get_ip_data(){   &nb...

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