How can the use of the header() function in PHP affect the handling of cookies on a webpage?

When using the header() function in PHP to set cookies, it's important to ensure that no output has been sent to the browser before calling the function. This is because headers must be sent before any actual content is sent to the browser. To avoid any issues with cookies not being set properly, make sure to call the header() function before any HTML or text output in your PHP script.

<?php
// Set cookie before any output
header('Set-Cookie: cookie_name=cookie_value; SameSite=None; Secure');
?>
<!DOCTYPE html>
<html>
<head>
    <title>Setting Cookies</title>
</head>
<body>
    <h1>Setting Cookies with PHP</h1>
</body>
</html>