Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

which is the better way to get the ip

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

getenv('REMOTE_ADDR'); 

or,

$_SERVER['REMOTE_ADDR'];

please tell me the difference, if any, between the two.

like image 902
Sachindra Avatar asked Oct 28 '09 11:10

Sachindra


1 Answers

$_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.

like image 99
Björn Avatar answered Sep 27 '22 23:09

Björn