How to get ip address of client machine using 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, sometimes this does not return the correct IP address of the visitor, 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.

getenv[] is used to get the value of an environment variable in PHP.

// Function to get the client IP address
function get_client_ip[] {
    $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[] {
    $ipaddress = '';
    if [isset[$_SERVER['HTTP_CLIENT_IP']]]
        $ipaddress = $_SERVER['HTTP_CLIENT_IP'];
    else if[isset[$_SERVER['HTTP_X_FORWARDED_FOR']]]
        $ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
    else if[isset[$_SERVER['HTTP_X_FORWARDED']]]
        $ipaddress = $_SERVER['HTTP_X_FORWARDED'];
    else if[isset[$_SERVER['HTTP_FORWARDED_FOR']]]
        $ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
    else if[isset[$_SERVER['HTTP_FORWARDED']]]
        $ipaddress = $_SERVER['HTTP_FORWARDED'];
    else if[isset[$_SERVER['REMOTE_ADDR']]]
        $ipaddress = $_SERVER['REMOTE_ADDR'];
    else
        $ipaddress = 'UNKNOWN';
    return $ipaddress;
}

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    What is an IP Address? 
    The IP address stands for Internet Protocol Address. An IP address is used to provide an identity to a networked device.IP addresses allow the location of different digital devices that are connected to the Internet to be pinpointed and differentiated from other devices.

    In this post we have discussed two different ways of determining the client IP address from a PHP script as explained below: 

    • Using getenv[] function: To get the IP Address,we use getenv[“REMOTE_ADDR”] command.
      The getenv[] function in PHP is used for retrieval of values of an environment variable in PHP.
      It is used to return the value of a specific environment variable.
      Syntax : 
       


    PHP

    Output : 

    Your IP is 127.1.1.0
    • Determining IP Address using $_SERVER Variable Method : There is another way to get the IP Address by using the $_SERVER[‘REMOTE_ADDR’] or $_SERVER[‘REMOTE_HOST’] variables. The variable in the $_SERVER array is created by the web server such as apache and those can be used in PHP.
      Basically $_SERVER[‘REMOTE_ADDR’]gives the IP address from which the request was sent to the web server. 
      Syntax : 

    PHP

    Output : 

    Your IP is 127.1.1.0

    by Vincy. Last modified on July 2nd, 2022.

    A single line of PHP script is sufficient to get IP address using PHP, ‘most of the times’.

    First I will present that one line PHP code, then show a tiny function which almost covers the rest of the cases. Followed by a discussion on privacy issues to get the IP address.

    This article has the following,

    1. One line script to get IP address using PHP.
    2. A simple function to get IP address in all the cases.
    3. Discussion on privacy issues in getting IP address.

    Get IP address using PHP in One Line

    
    

    $_SERVER is a PHP array which is set by the server. There is a possibility that this value may not have been set. Also be aware that, these headers can be easily spoofed by the users by setting an IP address themselves.

    Get IP address using PHP

    Following PHP script covers majority of the scenario and returns the user’s IP address.

    Chủ Đề