Categories: PHP

How to retrieve IP address in PHP

How To Get IP Address of User in PHP:-

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

PHP get IP Behind proxy

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();

How To Get Client Ip Address in PHP

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).

getenv() is used to get the value of an environment variable in PHP

// 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;
}

How To Get System Ip Address In PHP

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');

PHP get Remote Host Name:-

The Syntax is:

$ip = $_SERVER['REMOTE_ADDR'];
$hostname = gethostbyaddr($ip);

OR

$hostname = gethostbyaddr($_SERVER['REMOTE_ADDR']);
With these codes one can easily find the IP address of the respective user, system and many more with precise solution!

Also read our special section on PHP Interview Questions and Answers now!


Rohan pathak

Recent Posts

What Is a Progressive Web App? Why Would You Need One?

App usage is growing steadily without showing any signs of slowing down. Hence, it is no surprise that mobile applications…

1 year ago

7 Most Popular Paid Online Advertising Strategy

As the world has grown more digital, businesses have adapted themselves. An effectual adaptation includes online advertising. Offline advertising styles…

1 year ago

The Importance of User-Centered Design in Mobile App Development

Step into a world where apps dance to the user's tune. Picture Instagram, a photo-sharing sensation that swept the globe.…

1 year ago

Healthcare Mobile App Development: A Complete Guide for Founders

COVID-19 has led to a digitalization of lifestyle. As patients are taking their mental and physical health more seriously, healthcare…

1 year ago

Exploring Diverse WordPress Theme Niches: A Comprehensive Guide

Introduction WordPress, an immensely popular content management system (CMS), powers over 40% of the internet. What makes WordPress even more…

1 year ago

8 Awesome Blog Content Ideas for Movers to Skyrocket the SEO

For moving companies trying to capture their market share amidst stiff competition, a tip or two about what they can…

1 year ago