Search code examples
phpip-addressfetch

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.


Solution

  • getenv() can be used to access any environment variables (PHP simply registers REMOTE_ADDR as an environment variable for the script), while with $_SERVER you obviously only access the contents of the $_SERVER superglobal.

    The common approach is to use $_SERVER for this, although it doesn't really make a difference functionality-wise.