Home > Backend Development > PHP Tutorial > PHP implementation code to obtain the user's real IP and geographical location (Taobao IP interface)

PHP implementation code to obtain the user's real IP and geographical location (Taobao IP interface)

WBOY
Release: 2016-07-25 09:00:37
Original
995 people have browsed it
PHP obtains the user's real IP address and analyzes its actual geographical location based on the Taobao IP interface. Friends in need can refer to it.

The Taobao IP library is used in this article: http://ip.taobao.com.

The code is as follows.

<?php
/**
 * 获取用户真实 IP
 * 程序员之家 bbs.it-home.org
 */
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;
}
?>
Copy after login

>>>> Articles you may be interested in: PHP Sina interface to query IP geographical location php Tencent IP sharing plan to obtain IP geographical location php gets geographical location by IP PHP gets the geolocation code via IP Code sharing for php to obtain website location and operating system information An example reference for php to obtain geographical location through IP php gets current geographical location interface based on IP address



source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template