【问题标题】:How to get the page visitors Country with PHP? [closed]如何使用 PHP 获取页面访问者所在的国家/地区? [关闭]
【发布时间】:2012-10-09 13:39:53
【问题描述】:

对于我正在开发的网站,我希望它能够说出每个国家/地区有多少用户访问了我的网站,但我很好奇的是如何获取用户的国家/地区(从他们的 IP,也许?)?我已经数小时四处寻找 API,但找不到任何像样的东西。有人有什么建议吗?感谢您的帮助:)。

【问题讨论】:

    标签: php api ip country


    【解决方案1】:

    您可以使用外部 API,例如 geoplugin.net

    $xml = simplexml_load_file("http://www.geoplugin.net/xml.gp?ip=76.109.14.196");
    echo $xml->geoplugin_countryName ;
    

    输出国家

    United States
    

    完整的 XML 响应

    <geoPlugin>
    <geoplugin_request>76.109.14.196</geoplugin_request>
    <geoplugin_status>200</geoplugin_status>
    <geoplugin_city>West Palm Beach</geoplugin_city>
    <geoplugin_region>FL</geoplugin_region>
    <geoplugin_areaCode>561</geoplugin_areaCode>
    <geoplugin_dmaCode>548</geoplugin_dmaCode>
    <geoplugin_countryCode>US</geoplugin_countryCode>
    <geoplugin_countryName>United States</geoplugin_countryName>
    <geoplugin_continentCode>NA</geoplugin_continentCode>
    <geoplugin_latitude>26.761600494385</geoplugin_latitude>
    <geoplugin_longitude>-80.091598510742</geoplugin_longitude>
    <geoplugin_regionCode>FL</geoplugin_regionCode>
    <geoplugin_regionName>Florida</geoplugin_regionName>
    <geoplugin_currencyCode>USD</geoplugin_currencyCode>
    <geoplugin_currencySymbol>&#36;</geoplugin_currencySymbol>
    <geoplugin_currencyConverter>1</geoplugin_currencyConverter>
    </geoPlugin>
    

    获取IP的简单函数

    function getIP() {
        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'];
        }
        return $ip;
    }
    

    【讨论】:

    【解决方案2】:

    基本方法是记录传入的 IP 地址(在$_SERVER['REMOTE_ADDR']),然后使用地理定位数据库将其转换为国家信息。

    请记住,您需要使您的地理位置数据库保持最新状态。

    有几个地方提供地理定位数据库。一些成本(它们往往更准确和更新),但免费的通常也相当不错。

    【讨论】:

      【解决方案3】:

      有时我们需要根据访问者所在的国家/地区管理内容或货币。通常每个开发人员都会遇到这种情况,他们试图找到更好的解决方案。你可以在这里找到这篇文章的详细信息-http://virallangaliya.blogspot.in/2013/04/how-to-find-city-and-country-of-visitor.html

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-03-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-09-14
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多