How to get IP Address in PHP?

How to get IP Address in PHP: Sometimes the visitor’s IP address is needed for securities, validation, spam prevention, etc. If you want to get visitor’s IP address in PHP it’s very easy. Now you will wonder what IP address is?

IP address stands for Internet Protocol address. IP address being a numerical label is connected to computer which is even assigned for each and every device. It finds the use for communication as the protocol of internet. An IP address have two principal functions first is local addressing and second is network or host interface identification. IP address is the unique number codes that computers connected to internet use to communicate with one another and locate. 

IP address is defined as 32-bit number by Internet Protocol version 4(IPv4). Because of the depletion of the availability of IPv4 and growth of internet, IPV6 which is a new version came into existence. IPv4 mainly uses 128 bit which was developed in the year 19958 and later was standardized on the month of December in the tear 1998.

There are mainly two types of’ basic types of IP addresses like dynamic and static. A static IP address is for a single device which is always constant. These IP addresses are mainly used for frequently accessed devices and services.  Dynamic IP address required less configuration and maintenance.

How to get IP Address in PHP


The simplest way to get someone’s/client’s IP address by using the $_SERVER[‘REMOTE_HOST’] or $_SERVER[‘REMOTE_ADDR’]  variables. The variable in the $_SERVER array was created by the web server and can also be used in PHP.


  1. //Get the client ip address
  2. $ipaddress = $_SERVER[‘REMOTE_ADDR’]

Sometimes this does not give out accurate result of IP address of the visitor. It can be also like the user is behind a proxy and your apache server is behind a reverse proxy. To get the right Ip address you can use other methods too.

Below are two examples that you can use in your functions. Both of the examples are difference in equivalent and also how and from where the values are retrieved. From example 1 getenv() is used to get the whole value from the environment variables of PHP and example 2  $_SERVER is used to show the value from the web server.  

EXAMPLE 1:  “getenv() is used to get the value of an environment variable in PHP

  1. // Function to get the client ip address
  2. function get_client_ip_env() {
  3.     $ipaddress = ”;
  4.     if (getenv(‘HTTP_CLIENT_IP’))
  5.         $ipaddress = getenv(‘HTTP_CLIENT_IP’);
  6.     else if(getenv(‘HTTP_X_FORWARDED_FOR’))
  7.         $ipaddress = getenv(‘HTTP_X_FORWARDED_FOR’);
  8.     else if(getenv(‘HTTP_X_FORWARDED’))
  9.         $ipaddress = getenv(‘HTTP_X_FORWARDED’);
  10.     else if(getenv(‘HTTP_FORWARDED_FOR’))
  11.         $ipaddress = getenv(‘HTTP_FORWARDED_FOR’);
  12.     else if(getenv(‘HTTP_FORWARDED’))
  13.         $ipaddress = getenv(‘HTTP_FORWARDED’);
  14.     else if(getenv(‘REMOTE_ADDR’))
  15.         $ipaddress = getenv(‘REMOTE_ADDR’);
  16.     else
  17.         $ipaddress = ‘UNKNOWN’;
  19.     return $ipaddress;
  20. }

EXAMPLE 2: $_SERVER it is use here to show the array contains server variable created by the web server.

  1. // Function to get the client ip address
  2. function get_client_ip_server() {
  3. $ipaddress = ”;
  4. if ($_SERVER[‘HTTP_CLIENT_IP’])
    1. $ipaddress = $_SERVER[‘HTTP_CLIENT_IP’];
  5. else if($_SERVER[‘HTTP_X_FORWARDED_FOR’])
    1. $ipaddress = $_SERVER[‘HTTP_X_FORWARDED_FOR’];
  6. else if($_SERVER[‘HTTP_X_FORWARDED’])
    1. $ipaddress = $_SERVER[‘HTTP_X_FORWARDED’];
  7. else if($_SERVER[‘HTTP_FORWARDED_FOR’])
    1. $ipaddress = $_SERVER[‘HTTP_FORWARDED_FOR’];
  8. else if($_SERVER[‘HTTP_FORWARDED’])
    1. $ipaddress = $_SERVER[‘HTTP_FORWARDED’];
  9. else if($_SERVER[‘REMOTE_ADDR’])
    1. $ipaddress = $_SERVER[‘REMOTE_ADDR’];
  10. else
    1. $ipaddress = ‘UNKNOWN’;
  11. return $ipaddress;
  12. }

STEPS: to get IP address in PHP

This information and steps will help you to get your clints IP address who visits your website.

Just keep in mind some points:

  • Here use the PHP $_server as superglobal
  • Here use date() to know the date and time but yes this function is not necessary.
  • Ip is the table name, ip_id have three colum and user_ip, date.

 Step 1: you have to creat data base and PHP file(index.php)  


$con = mysqli_connect (“localhost”,”root”,””,”product”);


// if ($db)  {

//eco “connection ok”;




//echo mysqli_error($con,$db);



Step 2:  you just need to copy the index.php

Function getuserIP()


// Get real visitor IP behind cloudflare network






$client = @$_SERVER[‘HTTP_CLIENT_IP’];


$remote = $_SERVER[‘REMOTE_ADDR’];

if(filter_var($client, FILTER_VALIDATE_IP))


$ip = $client;


elseif(filter_var($forward, FILTER_VALIDATE_IP))


$ip = $forward;




$ip = $remote;


return $ip;


$user_ip = getUserIP();

echo $user_ip; // Output IP address [Ex:]

  • Step 3: now insert the date and IP in your table


$date = date(‘y-d-m H-i a’);

echo $inst = “INSERT into ip values(”,’$user_ip’,’$date’)”;



  • IP address provides end to end connectivity. there it helps in getting a fast and good connectivity
  • IP address provides fast routing.
  • IP address helps in locating the server from anywhere in the world. if you are present in a place then you can easily locate the other server location
  • IP address is easy to manage and assign. It become easier to track internet traffic and assign access to certain users based identification.
  • Static IP address is more stable for internet use as they can never change.
  • Finding IP address can help to search who all are checking your website. You also may need to know about the people who are checking your sites and here IP address plays a major role.
  • IP address also includes host identifier and network identifier. It helps in locating the network of the host.


As we have discussed the way of getting an IP address in PHP. IP address helps in finding someone’s identity and the location. Every networking activity has its own IP addresses. It is very needed to track activities for security reason.

To know more details about How to get IP Address in PHP visit our official website.

Leave a Comment

Your email address will not be published. Required fields are marked *