Home > Backend Development > PHP Tutorial > How to get the city based on IP address in php

How to get the city based on IP address in php

小云云
Release: 2023-03-21 17:50:02
Original
4987 people have browsed it

This article mainly shares with you how to obtain the city based on the IP address in PHP. This article is mainly in the form of code. I hope it can help everyone.

<?php
header(&#39;Content-Type:text/html;Charset=utf-8&#39;);
function GetIp(){
    $realip = &#39;&#39;;
    $unknown = &#39;unknown&#39;;
    if (isset($_SERVER)){
        if(isset($_SERVER[&#39;HTTP_X_FORWARDED_FOR&#39;]) && !empty($_SERVER[&#39;HTTP_X_FORWARDED_FOR&#39;]) && strcasecmp($_SERVER[&#39;HTTP_X_FORWARDED_FOR&#39;], $unknown)){
            $arr = explode(&#39;,&#39;, $_SERVER[&#39;HTTP_X_FORWARDED_FOR&#39;]);
            foreach($arr as $ip){
                $ip = trim($ip);
                if ($ip != &#39;unknown&#39;){
                    $realip = $ip;
                    break;
                }
            }
        }else if(isset($_SERVER[&#39;HTTP_CLIENT_IP&#39;]) && !empty($_SERVER[&#39;HTTP_CLIENT_IP&#39;]) && strcasecmp($_SERVER[&#39;HTTP_CLIENT_IP&#39;], $unknown)){
            $realip = $_SERVER[&#39;HTTP_CLIENT_IP&#39;];
        }else if(isset($_SERVER[&#39;REMOTE_ADDR&#39;]) && !empty($_SERVER[&#39;REMOTE_ADDR&#39;]) && strcasecmp($_SERVER[&#39;REMOTE_ADDR&#39;], $unknown)){
            $realip = $_SERVER[&#39;REMOTE_ADDR&#39;];
        }else{
            $realip = $unknown;
        }
    }else{
        if(getenv(&#39;HTTP_X_FORWARDED_FOR&#39;) && strcasecmp(getenv(&#39;HTTP_X_FORWARDED_FOR&#39;), $unknown)){
            $realip = getenv("HTTP_X_FORWARDED_FOR");
        }else if(getenv(&#39;HTTP_CLIENT_IP&#39;) && strcasecmp(getenv(&#39;HTTP_CLIENT_IP&#39;), $unknown)){
            $realip = getenv("HTTP_CLIENT_IP");
        }else if(getenv(&#39;REMOTE_ADDR&#39;) && strcasecmp(getenv(&#39;REMOTE_ADDR&#39;), $unknown)){
            $realip = getenv("REMOTE_ADDR");
        }else{
            $realip = $unknown;
        }
    }
    $realip = preg_match("/[\d\.]{7,15}/", $realip, $matches) ? $matches[0] : $unknown;
    return $realip;
}

function GetIpLookup($ip = &#39;&#39;){
    if(empty($ip)){
        $ip = GetIp();
    }
    $res = @file_get_contents(&#39;http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip=&#39; . $ip);
    if(empty($res)){ return false; }
    $jsonMatches = array();
    preg_match(&#39;#\{.+?\}#&#39;, $res, $jsonMatches);
    if(!isset($jsonMatches[0])){ return false; }
    $json = json_decode($jsonMatches[0], true);
    if(isset($json[&#39;ret&#39;]) && $json[&#39;ret&#39;] == 1){
        $json[&#39;ip&#39;] = $ip;
        unset($json[&#39;ret&#39;]);
    }else{
        return false;
    }
    return $json;
}


$ipInfos = GetIpLookup(&#39;123.125.114.144&#39;); //baidu.com IP鍦板潃
var_dump($ipInfos);
?>
Copy after login

Related recommendations:

PHP obtains the specific implementation of the city based on the IP address_PHP tutorial

The above is the detailed content of How to get the city based on IP address in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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