用PHP获取访者IP

Published
July 10, 2022
浏览次数 :  0

function getUserIpAddr(){
    if(!empty($_SERVER['HTTP_CLIENT_IP'])){
        //ip from share internet
        $ip = $_SERVER['HTTP_CLIENT_IP'];
    }elseif(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
        //ip pass from proxy
        $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
    }else{
        $ip = $_SERVER['REMOTE_ADDR'];
    }
    return $ip;
}

echo 'User Real IP - '.getUserIpAddr();

关于PHP全局变量

Superglobals are variables that are always accessible no matter what scope they have. They can also be accessed from any class, function without doing something special.

$_SERVER also belongs to the super global variables.It is capable of holding information about paths, headers, as well as script locations. The entities inside that array are generated by the webserver. There are also other superglobal variables such as $GLOBALS, $_REQUEST, $_POST, $_GET, $_FILES, $_ENV, $_COOKIE, and $_SESSION .

Top