如何使用 PHP 获取页面访问者国家/地区? [英] How to get the page visitors Country with PHP?

查看:31
本文介绍了如何使用 PHP 获取页面访问者国家/地区?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

For a website I'm developing, I want it to like say how many users from each country have visited my site, but what I'm curious is how to get the user's country (From their IP, Maybe?)? I've looked around for APIs for hours upon hours but I couldn't find anything decent. Does anyone have any recommendations? Thanks for the help :).

推荐答案

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

You can use external API's like 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;
}

这篇关于如何使用 PHP 获取页面访问者国家/地区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆