which is the better way to get the ip

前端 未结 6 1214
闹比i
闹比i 2020-12-21 05:30

What is the better way of getting the IP address in PHP:

getenv(\'REMOTE_ADDR\'); 

or,

$_SERVER[\'REMOTE_ADDR\'];
         


        
6条回答
  •  余生分开走
    2020-12-21 05:55

    $_SERVER is a built in PHP variable, while getenv() ask the environment (probably Apache/IIS) for values.

    The best way to get the IP is;

    $ip = (!empty($_SERVER['REMOTE_ADDR'])) ? $_SERVER['REMOTE_ADDR'] : getenv('REMOTE_ADDR');
    

    But I doubt there's any difference between these two variables... Hm.

提交回复
热议问题