
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!
When it comes to children, there’s one universal truth: the right toy can spark imagination, build skills, and make memories…
In today’s digital age, where screens and gadgets dominate our children’s lives, there’s something heartwarming about a well-loved plush toy…
In a world dominated by screens and fast-paced routines, it’s easy to forget the simple magic of a toy in…
In the heart of Delhi’s vibrant streets lies a world where imagination meets innovation — the magical universe of toys.…
When was the last time a toy truly amazed you—not just as a product, but as a thoughtful tool for…
In the digital age, the way we experience childhood has changed, but the essence remains the same—imagination, exploration, and joy.…