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

Modern Toys, Magical Moments: Why the Best Toy Shop in Noida Is More Than Just a Store

When it comes to children, there’s one universal truth: the right toy can spark imagination, build skills, and make memories…

6 months ago

Rediscovering Joy: A New Era of Creativity & Comfort in Toy Stores

In today’s digital age, where screens and gadgets dominate our children’s lives, there’s something heartwarming about a well-loved plush toy…

7 months ago

Unboxing Imagination: Discovering the Joy of Play at a Toy Store in Noida

In a world dominated by screens and fast-paced routines, it’s easy to forget the simple magic of a toy in…

7 months ago

Imagination Unboxed: Discover Joy at the Toy Shop in Delhi

In the heart of Delhi’s vibrant streets lies a world where imagination meets innovation — the magical universe of toys.…

7 months ago

Play with Purpose: Discovering the Ultimate Toy Store in Noida

When was the last time a toy truly amazed you—not just as a product, but as a thoughtful tool for…

7 months ago

From Tears to Toys: Exploring Modern Childhood through Delhi’s Favorite Toy Shop

In the digital age, the way we experience childhood has changed, but the essence remains the same—imagination, exploration, and joy.…

7 months ago