php获取当前ip
<?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);
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>pr查询</title>
</head>
<body style="text-align:center;margin:200px auto;font-size:25px">
<?php
echo '您来自: '.$contents->data->country.$contents->data->area.$contents->data->region.$contents->data->city.' ip为'.$contents->data->ip;
?>
</body>
</html>
另外在附一份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;
}