How to get IP address through php code.

Every php coder want to store IP address of user for tracking and other different purpose.

Here is the function which i am using in my code to store ip address in db.

    function userIPAdd()
    {
       //This returns the True IP of the client calling the requested page
       // Checks to see if HTTP_X_FORWARDED_FOR
       // has a value then the client is operating via a proxy
       $userIP = $_SERVER['HTTP_X_FORWARDED_FOR'];
       if($userIP == "")
      { 
         $userIP = $_SERVER['REMOTE_ADDR'];  
      }
      // return the IP we've figured out:
      return $userIP;
    }

You can use this simple php function in different ways.

Share

Leave a Reply

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