What best practices should be followed when setting cookies in PHP?
When setting cookies in PHP, it is important to follow best practices to ensure security and privacy. This includes setting the cookie parameters such as expiration time, path, domain, and secure flag appropriately. It is also recommended to sanitize and validate the cookie data to prevent any security vulnerabilities.
// Set a cookie with secure and HttpOnly flags
setcookie('user_id', '123', time() + 3600, '/', 'example.com', true, true);