Skip to content

Instantly share code, notes, and snippets.

@simplelife7
Created September 14, 2013 03:07
Show Gist options
  • Save simplelife7/6558501 to your computer and use it in GitHub Desktop.
Save simplelife7/6558501 to your computer and use it in GitHub Desktop.
【php】获取用户IP地址,通过淘宝IP库查地址
<?php
/**
* 获取用户真实 IP
*/
function getIP()
{
static $realip;
if (isset($_SERVER)){
if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])){
$realip = $_SERVER["HTTP_X_FORWARDED_FOR"];
} else if (isset($_SERVER["HTTP_CLIENT_IP"])) {
$realip = $_SERVER["HTTP_CLIENT_IP"];
} else {
$realip = $_SERVER["REMOTE_ADDR"];
}
} else {
if (getenv("HTTP_X_FORWARDED_FOR")){
$realip = getenv("HTTP_X_FORWARDED_FOR");
} else if (getenv("HTTP_CLIENT_IP")) {
$realip = getenv("HTTP_CLIENT_IP");
} else {
$realip = getenv("REMOTE_ADDR");
}
}
return $realip;
}
/**
* 获取 IP 地理位置
* 淘宝IP接口
* @Return: array
*/
function getCity($ip)
{
$url="http://ip.taobao.com/service/getIpInfo.php?ip=".$ip;
$ip=json_decode(file_get_contents($url));
if((string)$ip->code=='1'){
return false;
}
$data = (array)$ip->data;
return $data;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment