What are some best practices for handling cookies in PHP forms?
When handling cookies in PHP forms, it is important to sanitize and validate the cookie data to prevent any security vulnerabilities. Additionally, set the cookie expiration time appropriately to ensure it remains valid for the desired duration. Finally, use secure and HttpOnly flags when setting cookies to enhance security measures.
// Sanitize and validate cookie data
$cookie_value = filter_var($_COOKIE['cookie_name'], FILTER_SANITIZE_STRING);
// Set cookie expiration time
$expiration_time = time() + 3600; // Cookie expires in 1 hour
setcookie('cookie_name', $cookie_value, $expiration_time, '/', '', false, true);