Get Remote IP Address in PHP

<php
   function getRemoteIPAddress() 
   {
       $ip = $_SERVER['REMOTE_ADDR'];
       return $ip;
   }
?>

The code will not work if your client has a proxy server. In that case, use this function to get the real IP address of client.

<?php

   function getRealIPAddr()
   {
       //check ip from share internet
       if (!empty($_SERVER['HTTP_CLIENT_IP'])) 
       {
           $ip=$_SERVER['HTTP_CLIENT_IP'];
       }
       //to check ip is pass from proxy
       elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))  
       {
           $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
       }
       else
       {
           $ip=$_SERVER['REMOTE_ADDR'];
       }
       return $ip;
   }

?>

Do you find this helpful?

Related articles