PHP:如何使用IP范围查找美国国家


PHP: How to find US country by using IP range?

>我使用一些PHP脚本来检测美国IP,例如Geoplugin,hostip,...但它使我的页面变慢。那么,你能告诉我如何使用IP范围检测美国国家吗?谢谢!

您可以使用此代码找到

    $details = json_decode(file_get_contents("http://ipinfo.io/{$ip}"));
    $country_code = $details->country;

您可以从此 URL 获取 PHP 国家数组:- https://gist.github.com/DHS/1340150#comment-1244239 并保存到数据库中。现在,您只有一个查询即可根据国家/地区代码获取国家/地区名称。

ipinfo.io 是个

好主意!,现在可以用JQuery来对付用户了!

        if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
            $ip = $_SERVER['HTTP_CLIENT_IP'];
        } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
            $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
        } else {
            $ip = $_SERVER['REMOTE_ADDR'];
        }
        echo "
        <script type='"application/javascript'">
            $.ajax({
                 url:'http://ipinfo.io/".$ip."/json',
                 type:'"get'",
                 dataType:'"json'",
                 cache:'"false'",
                 success:function(result,status,xhr){
                    alert(result.country); 
                 }
             });
        </script>
        ";