Are there any best practices for using PHP to retrieve IP addresses?

When retrieving IP addresses using PHP, it is important to consider security and accuracy. To ensure security, always validate and sanitize the input to prevent any potential security vulnerabilities. To accurately retrieve the IP address of a user, consider using the $_SERVER['REMOTE_ADDR'] variable, which provides the IP address of the client accessing the server.

// Validate and sanitize the IP address
$ip = filter_var($_SERVER['REMOTE_ADDR'], FILTER_VALIDATE_IP);

// Output the IP address
echo "Your IP address is: " . $ip;