当前位置: 首页 > Web后端 > PHP > 正文

PHP获取用户的真实IP地址

2017-04-08 来源:CSDN博客 Coder_Designer
<?php

function getRealIpAddr() 
{ 
    if (!empty($_SERVER['HTTP_CLIENT_IP'])) 
    { 
        $ip=$_SERVER['HTTP_CLIENT_IP']; 
    } 
    elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) 
//to check ip is pass from proxy 
    { 
        $ip=$_SERVER['HTTP_X_FORWARDED_FOR']; 
    } 
    else 
    { 
        $ip=$_SERVER['REMOTE_ADDR']; 
    } 
    return $ip; 
} 

echo getRealIpAddr();

?>