Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
What is the proper syntax for setcookie() in PHP 7.3? I usually use setcookie() like this:
setcookie("genone", "genoneinfo", "$cookie_expiration_time","/","",1,1);
That works, but how do I add the samesite option? I have tried like this, but it fails with php errors:
setcookie("genone", "genoneinfo", "$cookie_expiration_time","/","",1,1,['samesite'=>'Lax']);
errors: PHP Warning: setcookie() expects at most 7 parameters, 8 given zzz.com/index.php on line 73, referer: https://zzz.com/
Thanks,
–
–
–
–
An alternative signature supporting an options array has been added. This signature supports also setting of the SameSite cookie attribute.
That means you only supply the first two arguments as you would in the old version and place the remaining ones in an array of options:
setcookie('genone', 'genoneinfo', [
'expires' => $cookie_expiration_time,
'path' => '/',
'domain' => '',
'secure' => true,
'httponly' => true,
'samesite' =>'Lax',
Here parameter names from the old version become array keys, as per their description in the docs:
An associative array which may have any of the keys expires, path, domain, secure, httponly and samesite. The values have the same meaning as described for the parameters with the same name.
–
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.