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

原创
2014/03/10 17:47
阅读数 1.2K
<?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];
    }
展开阅读全文
打赏
0
1 收藏
分享
加载中
更多评论
打赏
0 评论
1 收藏
0
分享
返回顶部
顶部