There are many reasons for obtaining IP address of the system for logging, targeting, redirecting, etc. IP information can be found in the $_SERVER array of the respective system. The easiest and most simplest way to get the visitor’s IP address is by reading the REMOTE_ADDR field within this $_SERVER array.
$userip = $_SERVER['REMOTE_ADDR']; // Get the client ip address
Sometimes REMOTE_ADDR does not returns the correct IP address of the user. The reason behind this is the use Proxy. In that situation, use the following code to get real IP address of user in PHP.
function GetUserIpAddress(){ if(!empty($_SERVER['HTTP_CLIENT_IP'])){ $ip = $_SERVER['HTTP_CLIENT_IP']; }elseif(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){ $ip = $_SERVER['HTTP_X_FORWARDED_FOR']; }else{ $ip = $_SERVER['REMOTE_ADDR']; } return $ip; } echo 'User Real IP - '.GetUserIpAddress();
The simplest way to get the visitor’s/client’s IP address is using the $_SERVER[‘REMOTE_ADDR’] or $_SERVER[‘REMOTE_HOST’] variables. However this does not return the correct IP address of the visitor if there are prxoy, firewall, VPN or any other software to redirect IP, so we can use some other server variables to get the IP address. The below both functions are equivalent with the difference only in how and from where the values are retrieved.
You can use any of the following 2 functions. Both the functions are equivalent with the difference only where and how they are run to get the specific values. The first one getenv()
is used to get the values from PHP’s environment variables and the second one $_SERVER
is used to get the values from the web server respectively(e.g. apache).
// Function to get the client ip address function get_client_ip_env() { $ipaddress = ''; if (getenv('HTTP_CLIENT_IP')) $ipaddress = getenv('HTTP_CLIENT_IP'); else if(getenv('HTTP_X_FORWARDED_FOR')) $ipaddress = getenv('HTTP_X_FORWARDED_FOR'); else if(getenv('HTTP_X_FORWARDED')) $ipaddress = getenv('HTTP_X_FORWARDED'); else if(getenv('HTTP_FORWARDED_FOR')) $ipaddress = getenv('HTTP_FORWARDED_FOR'); else if(getenv('HTTP_FORWARDED')) $ipaddress = getenv('HTTP_FORWARDED'); else if(getenv('REMOTE_ADDR')) $ipaddress = getenv('REMOTE_ADDR'); else $ipaddress = 'UNKNOWN'; return $ipaddress; }
$_SERVER is an array that contains server variables created by the web server.
// Function to get the client ip address function get_client_ip_server() { $ipaddress = ''; if ($_SERVER['HTTP_CLIENT_IP']) $ipaddress = $_SERVER['HTTP_CLIENT_IP']; else if($_SERVER['HTTP_X_FORWARDED_FOR']) $ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR']; else if($_SERVER['HTTP_X_FORWARDED']) $ipaddress = $_SERVER['HTTP_X_FORWARDED']; else if($_SERVER['HTTP_FORWARDED_FOR']) $ipaddress = $_SERVER['HTTP_FORWARDED_FOR']; else if($_SERVER['HTTP_FORWARDED']) $ipaddress = $_SERVER['HTTP_FORWARDED']; else if($_SERVER['REMOTE_ADDR']) $ipaddress = $_SERVER['REMOTE_ADDR']; else $ipaddress = 'UNKNOWN'; return $ipaddress; }
PHP has in built function to detect remote browser, IP address, and other various properties. You can use any one of the following statement to obtain the corresponding IP address from the system.
PHP Syntax to find System IP address
The Syntax is:
$ip = $_SERVER['REMOTE_ADDR'];
OR
$ip= $REMOTE_ADDR;
OR
$ip = $_SERVER['REMOTE_ADDR'];
OR
$ip = getenv('HTTP_CLIENT_IP');
The Syntax is:
$ip = $_SERVER['REMOTE_ADDR']; $hostname = gethostbyaddr($ip);
OR
$hostname = gethostbyaddr($_SERVER['REMOTE_ADDR']);
Also read our special section on PHP Interview Questions and Answers now!
App usage is growing steadily without showing any signs of slowing down. Hence, it is no surprise that mobile applications…
As the world has grown more digital, businesses have adapted themselves. An effectual adaptation includes online advertising. Offline advertising styles…
Step into a world where apps dance to the user's tune. Picture Instagram, a photo-sharing sensation that swept the globe.…
COVID-19 has led to a digitalization of lifestyle. As patients are taking their mental and physical health more seriously, healthcare…
Introduction WordPress, an immensely popular content management system (CMS), powers over 40% of the internet. What makes WordPress even more…
For moving companies trying to capture their market share amidst stiff competition, a tip or two about what they can…