What are the potential security risks of storing passwords and usernames in cookies in PHP?

Storing passwords and usernames in cookies in PHP can pose a significant security risk as cookies are easily accessible and can be tampered with by malicious users. It is recommended to store only a unique identifier in the cookie and keep the sensitive information on the server side. This way, even if the cookie is compromised, the actual credentials remain secure.

// Set a unique identifier in the cookie
$unique_id = generateUniqueID();
setcookie('user_id', $unique_id, time() + (86400 * 30), '/');

// Store the sensitive information securely on the server side
$user_id = $_COOKIE['user_id'];
$stored_username = getUsernameFromDatabase($user_id);
$stored_password = getPasswordFromDatabase($user_id);