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 : 
       

    How to get ip address of client machine using php

    PHP

     $ipaddress = getenv("REMOTE_ADDR") ;

     Echo "Your IP Address is " . $ipaddress;

    ?>

    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

     $ipaddress = $_SERVER['REMOTE_ADDR']

     Echo "Your IP Address is " . $ipaddress;

    ?>

    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.

    
    

    Rest of the article is for comprehensiveness. If you just want to get IP address using PHP, you can stop at this point. Copy the above script and carry on with your IP address journey.

    How to validate user’s IP address

    IP address can be easily spoofed. It can come from behind a proxy server. CloudFlare like proxies can be involved. Localhost can return invalid IP address. So, if you want to validate an IP address, use the below script.

    Flowchart to get IP address

    Following flowchart explains the flow of control to get IP address using PHP.

    How to get ip address of client machine using php

    Get user’s IP address for CloudFlare hosted websites

    When the website is hosted using CloudFlare service, $_SERVER['REMOTE_ADDR'] will return the CloudFlare server’s IP address and not the user’s original IP address. So in such a situation, you can use the variable set by the CloudFlare server to get the IP address.

    I have taken CloudFlare as example. There are many similar services. You need to talk to that service provider to know which request header they are setting and get the IP address accordingly.

    if (isset($_SERVER["HTTP_CF_CONNECTING_IP"])) {
            $_SERVER['REMOTE_ADDR'] = $_SERVER["HTTP_CF_CONNECTING_IP"];
            $ipAddress = $_SERVER['REMOTE_ADDR'];
    }
    

    IP address reliability

    $_SERVER['REMOTE_ADDR'] is the most reliable variable from the above list of variables. REMOTE_ADDR may not contain the real IP address of the TCP connection as well. It depends on the SAPI configuration.

    If the user is behind a proxy server, HTTP_X_FORWARDED_FOR will be set. But this can be easily spoofed by the user by setting an IP address he wishes to expose.

    Even the variable X-Forwarded-For or the Client-IP can be set by the user to any value he wishes.

    Take an example of a shopping cart. You may display the product price according to the user’s country. In such a scenario, you may use the user’s IP address to determine the user’s country. In such a scenario, the code to get user’s IP address should be fool-proof.

    IP address field length

    When you are storing the IP address in the database, remember to have the field size as 45 for single IP. Because there is a possibility for IPv6 IP addresses. In its full form the IPv6 IP address can be 45 characters in length.

    How to get the IP address of a host

    Using a hostname, we can get the server’s IP address using the PHP’s built-in function gethostbyname. This returns the IPv4 address.

    
    

    Output to get IP address for Google:

    Google’s IP Address is: 142.250.67.36

    Get hostname using the IP address

    The reverse is also possible by using gethostbyaddr().

    Privacy issues in getting users’ IP address

    Client’s IP address can be considered as private information in certain legislation. It depends on the land of law. You never know from which geographic region you will get users.

    As per GDPR (Article 4, Point 1; and Recital 49), IP address is a personal data. I would suggest to inform the user and get consent to log the IP address. Also, call it out explicitly in your “Privacy Policy” document.

    Explicitly state that you are collecting and logging the client user’s IP address and the reason for doing so.

    Conclusion

    A good understanding of the scenario in which you are going to get IP address of the user is important. Then understand that the end user can easily spoof his IP address. Third understand each server variables, from where the IP address is fetched. Last but not the least, explicitly call out in your privacy policy that you are getting the client user’s IP address.

    ↑ Back to Top

    How can you get the IP address of a client in PHP?

    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.

    How do I find my client IP address?

    Getting the Client IP Address.
    Use the system environment variable $_SERVER["REMOTE_ADDR"] . One benefit is that on Pantheon this takes into account if the X-Forwarded-For header is sent in cases when a request is filtered by a proxy..
    Use Drupal's ip_address() function..

    What is $_ server [' Remote_addr ']?

    $_SERVER['REMOTE_ADDR'] Returns the IP address from where the user is viewing the current page. $_SERVER['REMOTE_HOST'] Returns the Host name from where the user is viewing the current page. $_SERVER['REMOTE_PORT']

    How can I get IP address and store in database using PHP?

    You can try this one also. $ip=$_SERVER['REMOTE_ADDR']; echo "IP address= $ip"; If your application hosted on same machine from where you are trying to request it will always return '::1', It means LocalHost. else it will return client IP Address.