search
top

Get IP Address in PHP

Many times we need the IP Address of the User visiting our website in our Application.

I am listing here the method to get the User IP Address, hope it might be useful to many of you.

//function for getting IP
//start
function getuserip()
{

    if (isset($_SERVER))
   {
        if (isset($_SERVER["HTTP_X_FORWARDED_FOR"]))
            return $_SERVER["HTTP_X_FORWARDED_FOR"];

        if (isset($_SERVER["HTTP_CLIENT_IP"]))
            return $_SERVER["HTTP_CLIENT_IP"];

	        return $_SERVER["REMOTE_ADDR"];
    }

	    if (getenv('HTTP_X_FORWARDED_FOR'))
	        return getenv('HTTP_X_FORWARDED_FOR');

	    if (getenv('HTTP_CLIENT_IP'))
	        return getenv('HTTP_CLIENT_IP');

		    return getenv('REMOTE_ADDR');
}
//end

echo $user_ip = getuserip(); //call to the function


One Response to “Get IP Address in PHP”

  1. Interesting article, thanks for sharing it with us.

Leave a Reply

top