PHP 获取用户 IP 地址及位置信息 - 国家、城市、经纬度
可以使用以下 PHP 代码获取用户 IP 地址:
$ip = $_SERVER['REMOTE_ADDR'];
要获取用户所在地址和国家,可以使用 IP 地址查询服务。以下是一个使用 'ip-api.com' 服务的示例代码:
$ip = $_SERVER['REMOTE_ADDR'];
$json = file_get_contents('http://ip-api.com/json/' . $ip);
$details = json_decode($json);
$country = $details->country;
$city = $details->city;
$region = $details->regionName;
$zip = $details->zip;
$timezone = $details->timezone;
$isp = $details->isp;
$lat = $details->lat;
$lon = $details->lon;
注意:使用此服务可能会受到 IP 查询限制,建议查阅该服务的使用条款和限制。
原文地址: https://www.cveoy.top/t/topic/mOY6 著作权归作者所有。请勿转载和采集!