What is the recommended method for obtaining a user's IP address in PHP when implementing features like an IP log in a guestbook?

When implementing features like an IP log in a guestbook, the recommended method for obtaining a user's IP address in PHP is to use the $_SERVER['REMOTE_ADDR'] variable. This variable contains the IP address of the user making the request to the server.

// Get the user's IP address
$user_ip = $_SERVER['REMOTE_ADDR'];

// Log the user's IP address
$log_message = "User IP: " . $user_ip;
file_put_contents('ip_log.txt', $log_message . PHP_EOL, FILE_APPEND);