How can the "Cannot modify header information" error be resolved when using setcookie in PHP?
The "Cannot modify header information" error in PHP occurs when there is output sent to the browser before calling the setcookie function. To resolve this issue, make sure there is no output (such as HTML, whitespace, or error messages) before setting the cookie.
<?php
ob_start(); // Start output buffering
setcookie("cookie_name", "cookie_value", time() + 3600, "/");
ob_end_flush(); // Flush output buffer and send cookie
?>