Home > Article > Backend Development > Get the visitor's real IP address in php_PHP tutorial
php gets the visitor’s real IP address Sometimes we need to get the user's real IP address, for example, logging, geolocation, user information, website data analysis, etc. In fact, getting the IP address is very simple, just $_SERVER['REMOTE_ADDR'].
php tutorial to get the visitor’s real ip address
Sometimes we need to get the user's real IP address, for example, logging, geolocation, user information, website data analysis, etc. In fact, getting the IP address is very simple, just $_server['remote_addr'].
*/
//The simplest method
$ip = $_server['remote_addr'];
//As long as you use a proxy in the above method, you cannot get the real IP address. There are more detailed methods below
echo "remote addr: " . $_server['remote_addr']."
";
echo "x forward: " . $_server['http_x_forwarded_for']."
";
echo "clien ip: " . $_server['http_client_ip']."
";
//Okay, let’s look at an example.
function getip() {
$ip = $_server['remote_addr'];
if (!empty($_server['http_client_ip'])) {
$ip = $_server['http_client_ip'];
} elseif (!empty($_server['http_x_forwarded_for'])) {
$ip = $_server['http_x_forwarded_for'];
}
Return $ip;
}
/*
If it is an encrypted proxy, the real IP address cannot be obtained.