PHP has a $ _SERVER variable that holds information related to the server. Through this variable, we can easily get the IP address as well.

Get IP Address in PHP – When creating a project, we have to save the IP address of the user several times by which we can perform different work. In PHP it is very easy to get the IP address of the user.

Php Contains a $ _SERVER variable that holds information related to the server. Through this variable, we can easily get IP address as well.

In most websites, we get IP address to track the user’s activities or to get limited access to a user also get IP address.

Get an IP address in PHP

The simplest way to get IP address is $ _SERVER in variable REMOTE_ADDR by index pass. Let us see an example.

<?php  
echo 'IP address of user :  '.$_SERVER['REMOTE_ADDR'];  
?>  

$ _SERVER[‘REMOTE_ADDR’] : This code gives us the IP address of the current user.

output

IP address of user: :: 1

In the above method, you will get the IP address until the user is using proxy or VPN. If the user is using proxy or VPN then you will have to write a bit more code.

<?php  
function getUserIpAddress() {  
	//When user uses share internet
	if(!empty($_SERVER['HTTP_CLIENT_IP'])) {  
	        $ip = $_SERVER['HTTP_CLIENT_IP'];  
	}  
	//when user uses proxy
	elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {  
	        $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];  
	}  
	//whether user uses simple internet without proxy and share
	else{  
	     $ip = $_SERVER['REMOTE_ADDR'];  
	}  
	return $ip;  
}  
$ip = getUserIpAddress();  
echo 'User Real IP Address - '.$ip;  
?>  

Output

User Real IP Address – :: 1

In the above code, you will get different different types of IP addresses. If user is shared by someone else Internet Even when using, you will get the IP address of that user.

If the user is using a VPN, you will still get the IP address of that user and I have also attached the code to get a simple IP address so that you can get the IP address by all means.

How to get IP address of website in PHP?

There is a function available in PHP to get the IP address of any website. gethostbyname (“www.example.com”);

With this function, you can easily get the IP address of any website, but now one thing to note here is that sometimes you will get the IP address of the website changed, so there is nothing to worry about.

This is seen because for the security of the website, there is always an IP address change and the IP address change process also depends on different different option like a hosting change or add any type of security in the hosting. etc.

<?php  
$ip_address = gethostbyname("www.google.com");  
echo "IP Address of Google is - ".$ip_address;  
echo "</br>";  
$ip_address = gethostbyname("www.quofact.com");  
echo "IP Address of javaTpoint is - ".$ip_address;  
?>  

Conclusion and Final Words

You can store the IP address of every user visiting your website by the code given above and store it in your database. You can complete the tracking process of the user through the IP address or you can find the IP address to give some limited user access to your website.