How to get the IP address of the client/user in PHP?

by bria_metz , in category: Technology , 4 years ago

How to get the IP address of the client/user in PHP?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by zella , 4 years ago

You can use $_SERVER['REMOTE_ADDR'] to get IP address of user/client in PHP, But sometime it may not return the true IP address of the client at all time. Use Below code to get true IP address of user.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
function getTrueIpAddr(){
 if (!empty($_SERVER['HTTP_CLIENT_IP']))   //check ip from share internet
    {
      $ip=$_SERVER['HTTP_CLIENT_IP'];
    }
elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))   //to check ip is pass from proxy
  {
      $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
  }
    else
  {
      $ip=$_SERVER['REMOTE_ADDR'];
  }
    return $ip;
}


Related Threads:

What Are the Best Practices for Writing Secure Oracle Sql Queries?
What Are the Key Components Of a Winning Proposal in 2025?
How to get length of an array in PHP ?
How to get no of arguments passed to a PHP Function?
What Are the Best Platforms to Earn Money by Freelancing?
How can you get web browser’s details using PHP?