如何使用PHP获取国家/地区的页面访问者?
对于我正在开发的一个网站,我希望它能告诉我每个国家有多少用户访问过我的网站,但我好奇的是如何获得用户的国家(可能是他们的IP?)?我找了好几个小时的API,但找不到任何像样的。有人有什么建议吗?感谢您的帮助:)。您可以使用类似的外部API 输出国如何使用PHP获取国家/地区的页面访问者?,php,api,ip,country,Php,Api,Ip,Country,对于我正在开发的一个网站,我希望它能告诉我每个国家有多少用户访问过我的网站,但我好奇的是如何获得用户的国家(可能是他们的IP?)?我找了好几个小时的API,但找不到任何像样的。有人有什么建议吗?感谢您的帮助:)。您可以使用类似的外部API 输出国 United States 完整XML响应 <geoPlugin> <geoplugin_request>76.109.14.196</geoplugin_request> <geoplugin_status
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>$</geoplugin_currencySymbol>
<geoplugin_currencyConverter>1</geoplugin_currencyConverter>
</geoPlugin>
基本方法是记录传入的IP地址(它位于
$\u服务器['REMOTE\u ADDR']
),然后使用地理位置数据库将其转换为国家信息
记住,您需要保持地理位置数据库的最新状态
有几个地方提供地理定位数据库。有些成本(它们往往更准确、更及时),但免费的通常也相当不错。有时我们需要根据访问者的国家管理内容或货币。通常这种情况会出现在每个开发人员身上,他们会试图找到更好的解决方案。你可以在这里找到详细的文章-啊!我想一定有这样的事!感谢您的回答,这真的很有帮助:)“HTTP_X_FORWARDED_for不应被用作验证用户IP的手段”
<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>$</geoplugin_currencySymbol>
<geoplugin_currencyConverter>1</geoplugin_currencyConverter>
</geoPlugin>
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;
}