获取ip地址

function getUserRealIP() {
    $headers = array(
        'HTTP_CLIENT_IP',
        'HTTP_X_FORWARDED_FOR',
        'HTTP_X_FORWARDED',
        'HTTP_X_CLUSTER_CLIENT_IP',
        'HTTP_FORWARDED_FOR',
        'HTTP_FORWARDED',
        'REMOTE_ADDR'
    );
 
    foreach ($headers as $header) {
        if (isset($_SERVER[$header]) && filter_var($_SERVER[$header], FILTER_VALIDATE_IP)) {
            return $_SERVER[$header];
        }
    }
 
    return $_SERVER['REMOTE_ADDR'];
}
 
$userIP = getUserRealIP();
echo $userIP;

 

posted @ 2024-07-10 15:17  祈愿仙帝  阅读(4)  评论(0编辑  收藏  举报