zoukankan      html  css  js  c++  java
  • 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];
        }
  • 相关阅读:
    JavaScript数据结构——模仿ES6中定义的类似的Set类
    webpack简单的项目配置发生的错误
    三国
    把握中国经济的大局与动力
    人生道路
    C++
    https://计算机等级分类总结
    https://计算机四级
    https://计算机三级
    https://计算机二级
  • 原文地址:https://www.cnblogs.com/johnfu/p/3514043.html
Copyright © 2011-2022 走看看