PHP function showing IP Address

This article covers the available ways to get IP address using PHP script. IP address is unique address for the machines like client, server or proxy. In PHP, we can get IP address in two ways. These are,

  • By accessing $_SERVER global array.
  • By using getenv() function.

Both of the above two methods require some key values to get IP address of the user. We can retrieve machine address by sending appropriate key value to any one of the above PHP controls. For example, If we want to get client IP address, then we need to call either getenv(‘HTTP_CLIENT_IP’) or $_SERVER[‘HTTP_CLIENT_IP’].

Now, let us discuss about the above two method of showing IP address in detail.

php_show_ipaddress

By accessing $_SERVER global array.

In this method we are attempting to access IP address as server variable that are created remotely. These variables are stored in PHP’s $_SERVER global array.

This global array expect various key indexes to get IP address. Those possible key indexes are listed below.

  • HTTP_CLIENT_IP
  • HTTP_X_FORWARDED_FOR
  • HTTP_X_FORWARDED
  • HTTP_FORWARDED_FOR
  • HTTP_FORWARDED
  • REMOTE_ADDR

Following PHP program is used to get the IP address of the user by using above possible reserved keys as the index of $_SERVER array.

function showIPAddress() {
$variableIndex = array("HTTP_CLIENT_IP","HTTP_X_FORWARDED_FOR","HTTP_X_FORWARDED","HTTP_FORWARDED_FOR","HTTP_FORWARDED","REMOTE_ADDR");
for($i=0;$i<count($variableIndex);$i++){
if(!isset($ipAddress)) {
if(array_key_exists($variableIndex[$i], $_ENV)) {
$ipAddress = $_SERVER[$variableIndex[$i]];
echo $ipAddress;
break;
}
}
}
}
showIPAddress();

In the above PHP program $variableIndex array holds all possible reserved keys to find IP address. This array will be iterated to use each of its value in $_SERVER array, until $ipAddress variable is set. Once $ipAddress variable is set, we can break the loop.

By using getenv() function.

Instead of accessing server variables, this second method is used to get environment variable by using PHP getenv() menthod.

By using the same set of PHP reserved keys, IP address will be found by using getenv() function. So, in the above program, we are required to replace $_SERVER[] with getenv() function call. Now, the program will be as shown below.

function showIPAddress() {
$variableIndex = array("HTTP_CLIENT_IP","HTTP_X_FORWARDED_FOR","HTTP_X_FORWARDED","HTTP_FORWARDED_FOR","HTTP_FORWARDED","REMOTE_ADDR");
for($i=0;$i<count($variableIndex);$i++){
if(!isset($ipAddress)) {
if(array_key_exists($variableIndex[$i], $_SERVER)) {
$ipAddress = getenv($variableIndex[$i]);
echo $ipAddress;
break;
}
}
}
}
showIPAddress();

$_SERVER Vs getenv()

Though, both method are used to get IP address, except case-insensitivity getenv() function on accepting PHP reserved keys like, HTTP_CLIENT_IP. So, if we are not sure about the case of the reserved key indexes, then, accessing environment variable would be preferred method.

This PHP code tutorial was published on May 29, 2013.

↑ Back to Top