简体   繁体   English

如何通过PHP中的IP地址检索城市和国家

[英]How to retrieve city and country through IP address in PHP

I am trying to make a log of users heading to my website. 我正在尝试记录前往我的网站的用户的日志。 I want to save their IP addresses, as well as city and country info. 我要保存他们的IP地址以及城市和国家/地区信息。

Thanks in Advance. 提前致谢。

Try using MaxMind GeoIP database which I find the best solution that's freely available. 尝试使用MaxMind GeoIP数据库,该数据库可以免费找到最佳解决方案。 It has a set of methods to retrieve the city and the country by IP and to get the ip just use: $_SERVER['REMOTE_ADDR'] 它具有一组通过IP检索城市和国家并仅使用ip即可获得IP的方法: $_SERVER['REMOTE_ADDR']

You can get the IP from $_SERVER['REMOTE_ADDR'] . 您可以从$_SERVER['REMOTE_ADDR']获取IP。 To get location you will have to find some service that provides lookup and check the IP you just collected. 要获得位置,您将必须找到一些提供查找并检查您刚收集的IP的服务。

You might also consider signing up for Google analytics. 您也可以考虑注册Google Analytics(分析)。

I Think you should go for Google analytic, All these data you will get from there. 我认为您应该进行Google分析,所有这些数据都将从那里获得。 Otherwise for ip address you can use this script 否则,对于IP地址,您可以使用此脚本

function getIP() {
$ip;
if (getenv("HTTP_CLIENT_IP"))
$ip = getenv("HTTP_CLIENT_IP");
else if(getenv("HTTP_X_FORWARDED_FOR"))
$ip = getenv("HTTP_X_FORWARDED_FOR");
else if(getenv("REMOTE_ADDR"))
$ip = getenv("REMOTE_ADDR");
else
$ip = "UNKNOWN";
return $ip;
}

& for country name &国家名称

http://geoip.wtanaka.com/cc/$ipaddr

this will return country ISO code. 这将返回国家/地区ISO代码。

Do the users input their City and Country? 用户是否输入他们的城市和国家? If yes then easily save the data to database. 如果是,则轻松地将数据保存到数据库。

Or do you want to track these by IP? 还是要通过IP跟踪这些信息? If this one - download/buy the database of the countries by ip from http://www.ip2country.net/download.html and only put the IP into the database. 如果是这样,请从http://www.ip2country.net/download.html通过ip下载/购买国家/地区的数据库,然后仅将IP放入数据库中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM