PHP Program to get the client IP address


October 21, 2023, Learn eTutorial
1157

What is IP address?

An IP address is a unique address(numerical label) that identifies a device on the internet or a local network. IP means "Internet Protocol," which is a collection of rules that make a format to send information using the Internet. 

An IP address has two different functions to do

  • Host identification 
  • Location addressing

What is IPv4 and IPv6 in IP address?

Internet Protocol version 4 (IPv4) defines an IP address as a 32-bit number. However, with the increase in Internet usage and the shortage of available IPv4 addresses, a new version of IP started to be used which is called as version 6, IPv6. IPv^ is using 128 bits for the IP address, was developed in 1995, and standardized in December 1998.

How IP address is denoted?

Usually, an IP address is written in a human-readable format, such as 172.16.254.2 in IPv4, and 2001:db8:0:1234:0:567:8:1 in IPv6.

Who is maintaining the IP address?

IANA (Internet Assigned Numbers Authority) is the international authority to manage IP addresses worldwide, also by five regional Internet registries. These are the authorities that give the IP address to their regional internet service providers (ISPs).  Each ISP assigns an IP address to each device connected to its network.

How do you get a Client IP address using a PHP program?

To get the IP address of a user who is using a website is very simple in PHP as the PHP provides a variable called PHP $_SERVER to get the user's IP without much effort. We can use this variable to check who is using our website and if anyone is doing any wrong things on our website.

In this PHP program First, we have to check from where this IP address is coming from,

First, we have to check whether it is coming from a shared internet if so we have to use  HTTP_CLIENT_IP with the PHP $_SERVER variable to get the IP address into a variable

$ip_address = $_SERVER['HTTP_CLIENT_IP'];

Second, we have to check whether the IP address is form a proxy server and if so, we have to use HTTP_X_FORWARDED_FOR with PHP $_SERVER variable to get the IP address to a variable 

$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];  

third method to get the visitor's remote IP address is to use the REMOTE_ADDRwe are using the query  

$ip_address = $_SERVER['REMOTE_ADDR'];

to get the remote IP address of the user and save it to the variable.

Finally, we have to echo the variable which has the IP address of the user.

ALGORITHM

STEP 1: Check if the IP is from shared internet, then assign the IP to the variable.

STEP 2: Check if the IP is from the proxy, then assign the IP to the variable.

STEP 3: Check if the IP is from a remote address, then assign the IP to the variable.

STEP 4: Display the value of the variable.

PHP Source Code

                                          <?php
//check whether ip is from shared internet
if (!empty($_SERVER['HTTP_CLIENT_IP']))   
  {
    $ip_address = $_SERVER['HTTP_CLIENT_IP'];
  }
//check whether ip is from proxy
elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))  
  {
    $ip_address = $_SERVER['HTTP_X_FORWARDED_FOR'];
  }
//whether ip is from remote address
else
  {
    $ip_address = $_SERVER['REMOTE_ADDR'];
  }
echo $ip_address;
?>
                                      

OUTPUT

192.168.1.120