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, I'm a beginning programmer and do not understand the syntax... it looks like I need to use a whole bunch of brackets... Also, they declare all of the variable types in the example... is that required? I've never declared variable types in php... – nrsbus Mar 16, 2020 at 23:57 Brackets in docs denote optional parameters. Types are there so you know what kind of input is accepted for that parameter. Type declarations are meant only for function definitions, not when you call them. You just pass what you need like you did in your code sample in the question, you just have to use the alternative syntax. – El_Vanja Mar 17, 2020 at 0:00 Like this? setcookie ( "genone" [, "genoneinfo" [, "$cookie_expiration_time" [, "/" [, "" [, 1 [, 1 ]]]]]] ) : bool Then I have to add the samesite thing... Like this?: setcookie ( "genone" [, "genoneinfo" [, "$cookie_expiration_time" [, "/" [, "" [, 1 [, 1 [, "SameSite=Lax" ]]]]]]] ) : bool – nrsbus Mar 17, 2020 at 0:01 No. As I said, brackets in documentation just mean "optional parameter", as in "you can skip this parameter if it's ok to you to use defaults". – El_Vanja Mar 17, 2020 at 0:13

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.

You are awesome... thank you so much!!! I am reading through the example in the php manual now and comparing to your example so I can fully understand what's happening... thanks again! Much appreciated! – nrsbus Mar 17, 2020 at 0:17

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.