What are best practices for setting cookies in PHP to ensure compatibility across different browsers and devices?
When setting cookies in PHP, it is important to ensure compatibility across different browsers and devices. One best practice is to set the cookie path to '/' to make it accessible across the entire domain. Additionally, use the 'secure' flag for cookies that should only be transmitted over HTTPS. Finally, consider setting the 'SameSite' attribute to 'Strict' or 'Lax' to improve security and privacy.
// Set a cookie with path set to '/'
setcookie('cookie_name', 'cookie_value', time() + 3600, '/');
// Set a secure cookie
setcookie('secure_cookie', 'secure_value', time() + 3600, '/', '', true);
// Set a cookie with SameSite attribute set to 'Strict'
setcookie('samesite_cookie', 'samesite_value', time() + 3600, '/', '', false, true);