What are the potential security risks of storing data in cookies in PHP?

Storing sensitive data in cookies in PHP can pose security risks as cookies are stored on the client-side and can be easily accessed or manipulated by users. To mitigate this risk, it is recommended to encrypt the data before storing it in cookies.

// Encrypt data before storing in cookies
$secret_key = "my_secret_key";
$data = "sensitive_data_to_encrypt";
$encrypted_data = openssl_encrypt($data, 'AES-256-CBC', $secret_key, 0, 'my_secret_iv');
setcookie('encrypted_data', $encrypted_data, time() + (86400 * 30), '/');