How can cookies be used in PHP to store data securely, and what are the potential risks associated with storing sensitive information in cookies?

To store data securely in cookies in PHP, it is recommended to encrypt the sensitive information before storing it. This can help prevent unauthorized access to the data stored in the cookies. It is important to be cautious when storing sensitive information in cookies as they can be vulnerable to attacks such as cross-site scripting (XSS) and cross-site request forgery (CSRF).

// Encrypt sensitive data before storing it in a cookie
$sensitiveData = "sensitive information";
$encryptedData = openssl_encrypt($sensitiveData, 'AES-256-CBC', 'secret_key', 0, '16_character_iv');
setcookie('secure_cookie', $encryptedData, time() + 3600, '/', '', true, true);