php获取当前ip
程序员文章站
2022-05-09 23:36:34
...
php获取当前ip
参考:https://github.com/liu21st/thinkphp/blob/master/ThinkPHP/Library/Org/Net/IpLocation.class.php
$user_IP = ($_SERVER["HTTP_VIA"]) ? $_SERVER["HTTP_X_FORWARDED_FOR"] : $_SERVER["REMOTE_ADDR"];
$user_IP = ($user_IP) ? $user_IP : $_SERVER["REMOTE_ADDR"];
$URL='http://ip.taobao.com/service/getIpInfo.php?ip='.$user_IP;
$fcontents = file_get_contents("$URL");
$contents=json_decode($fcontents);
?>
pr查询
echo '您来自: '.$contents->data->country.$contents->data->area.$contents->data->region.$contents->data->city.' ip为'.$contents->data->ip;
?>
另外在附一份tp自带的吧:参考:https://github.com/liu21st/thinkphp/blob/master/ThinkPHP/Library/Org/Net/IpLocation.class.php
/*
* 根据ip地址获取所在城市
*/
protected function checkUserArea(){
import("ORG.Net.IpLocation");
$myip=get_client_ip();
$Ip=new IpLocation("UTFWry.dat");//导入ip库 参考http://thinkphp.cn/extend/270.html 把下载的文件放在Lib\ORG\Net下
$area=$Ip->getlocation($myip);
return $area;
}
AD:真正免费,域名+虚机+企业邮箱=0元