Home >php教程 >php手册 >PHP获取用户真实 IP , 淘宝IP接口获得ip地理位置

PHP获取用户真实 IP , 淘宝IP接口获得ip地理位置

WBOY
WBOYOriginal
2016-06-21 08:50:51895browse

自己不需ip库,免更新。 
淘宝IP库: http://ip.taobao.com 
多谢5楼提醒

 

/**

 * 获取用户真实 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;

}



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn