PHP判断用户所在国家并跳转对应的目录

<?php
    // 淘宝API查询国家代码
    $url = "http://ip.taobao.com/service/getIpInfo.php?ip=".get_client_ip();
    $json = json_decode(file_get_contents($url));
    $country = $json->{"data"}->{"country_id"};
    
    // 判断国家代码 把需要判断的国家加在下面就行了
    $countrys = array("US", "SE", "NZ", "NO", "IT", "UK", "DK", "CA", "BR", "AU", "CH", "CN");
    
    // 用meta标签跳转防止被拦截
    if (in_array($country, $countrys))
    {
        echo '<meta http-equiv="Refresh" content="0;url=/'.strtolower($country).'" />';
    }
    else
    {
        echo '<meta http-equiv="Refresh" content="0;url=/none" />';
    }

    // 获取IP地址
    function get_client_ip($type = 0) {
        $type       =  $type ? 1 : 0;
        static $ip  =   NULL;
        if ($ip !== NULL) return $ip[$type];
        if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
            $arr    =   explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
            $pos    =   array_search('unknown',$arr);
            if(false !== $pos) unset($arr[$pos]);
            $ip     =   trim($arr[0]);
        }elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
            $ip     =   $_SERVER['HTTP_CLIENT_IP'];
        }elseif (isset($_SERVER['REMOTE_ADDR'])) {
            $ip     =   $_SERVER['REMOTE_ADDR'];
        }
        // IP地址合法验证
        $long = sprintf("%u",ip2long($ip));
        $ip   = $long ? array($ip, $long) : array('0.0.0.0', 0);
        return $ip[$type];
    }

转载于:https://my.oschina.net/johnfu/blog/206917

猜你喜欢

转载自blog.csdn.net/weixin_34023863/article/details/91809133