Menu

How to tell PHP to use SameSite=None for cross-site cookies?

Written by

To set the SameSite attribute to “None” for cross-site cookies in PHP, you need to use the “setcookie” function and pass the appropriate options as an argument. The SameSite attribute prevents cookies from being sent in cross-site requests, which helps improve security and privacy.

Here’s an example of how you can set a cookie with the SameSite attribute set to “None” for cross-site cookies:

$cookieName = "example_cookie";
$cookieValue = "your_cookie_value";
$expiration = time() + (86400 * 30); // Cookie will expire in 30 days
$cookieOptions = [
    'expires' => $expiration,
    'path' => '/',
    'domain' => 'yourdomain.com', // Set your actual domain
    'secure' => true, // Cookie will only be sent over HTTPS
    'httponly' => true, // Cookie can't be accessed through JavaScript
    'samesite' => 'None', // SameSite attribute set to "None"
];

setcookie($cookieName, $cookieValue, $cookieOptions);

 

Article Categories:
PHP

Leave a Reply

Your email address will not be published. Required fields are marked *

Shares