How to get client IP Address in PHP

How to get client IP Address in PHP, if you want to get ip address in php the simplest way use to remote address server super global. But this is not the best way to do is using different server super global functions, http client IP and http x forwarded for, remote address. By using these three functions we can get correct ip address of a client or computer who is accessing your website.

By using this single line of function you can get client IP address in php.

// Get the client IP address
$ip = $_SERVER['REMOTE_ADDR']

But this is not the best way to php get client IP address, we can use different server super global functions to check valid IP address.(http client IP and http x forwarded for, remote address)

$http_client_ip = $_SERVER['HTTP_CLIENT_IP']; //Internet ip address
$http_x_forwarded_for = $_SERVER['HTTP_X_FORWARDED_FOR']; //checking for proxy server
$remote_addr = $_SERVER['REMOTE_ADDR']; //

if(!empty($http_client_ip)){
	$ip = $http_client_ip;
}elseif(!empty($http_x_forwarded_for)){
	$ip = $http_x_forwarded_for;
}else{
	$ip = $remote_addr;
}

echo $ip;

$_SERVER[‘HTTP_CLIENT_IP’] : This function will be used to get ip address of internet not the local ip address or ip address which is assigned by router.

$_SERVER[‘HTTP_X_FORWARDED_FOR’] : This function will be used to find client computer is using proxy server or not.

$_SERVER[‘REMOTE_ADDR’] : This is the remote ip address of the computer.

I hop this article helped you to find ip address in php, if you have any problem while using this code just let me know by using comment form.

Incoming search terms:

  • 2688:58628:9cbf8f1f243a8da6:http
Vivek Vengala
 

Vivek Vengala is a Online Entrepreneur, Web Developer from Hyderabad India.

Click Here to Leave a Comment Below 0 comments

Leave a Reply:

x

Join My Facebook Group to Receive Coding Tutorial for Free

email-subsribe