What potential security risks are present in the current login system implementation using cookies?

The potential security risk in the current login system implementation using cookies is that the cookies can be easily tampered with or stolen, leading to unauthorized access to user accounts. To mitigate this risk, we can implement cookie encryption and validation to ensure the integrity and confidentiality of the cookie data.

// Encrypt and set the cookie
function setEncryptedCookie($name, $value, $expiry) {
    $key = 'secret_key'; // Change this to a secure key
    $encryptedValue = openssl_encrypt($value, 'AES-256-CBC', $key, 0, '1234567890123456');
    setcookie($name, $encryptedValue, $expiry, '/', '', true, true);
}

// Decrypt and get the cookie value
function getDecryptedCookie($name) {
    $key = 'secret_key'; // Change this to a secure key
    if(isset($_COOKIE[$name])) {
        $decryptedValue = openssl_decrypt($_COOKIE[$name], 'AES-256-CBC', $key, 0, '1234567890123456');
        return $decryptedValue;
    }
    return null;
}

// Example usage
setEncryptedCookie('user_id', '123', time() + 3600); // Set encrypted cookie
$user_id = getDecryptedCookie('user_id'); // Get decrypted cookie value