What are some best practices for ensuring the $_SERVER['REMOTE_ADDR'] variable returns the correct IP address in PHP scripts?
The issue with $_SERVER['REMOTE_ADDR'] is that it can sometimes return the IP address of a proxy server rather than the actual client's IP address. To ensure that the correct IP address is returned, it is recommended to check for additional headers like 'HTTP_X_FORWARDED_FOR' and 'HTTP_CLIENT_IP' to get the client's IP address.
if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
$ip = $_SERVER['HTTP_CLIENT_IP'];
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
$ip = $_SERVER['REMOTE_ADDR'];
}