php 7.3 中 setcookie() 的正确语法是什么?
What is the proper syntax for setcookie() in php 7.3?
PHP7.3 中 setcookie() 的正确语法是什么?我通常这样使用 setcookie():
setcookie("genone", "genoneinfo", "$cookie_expiration_time","/","",1,1);
可以,但是如何添加 samesite 选项?我试过这样,但失败并出现 php 错误:
setcookie("genone", "genoneinfo", "$cookie_expiration_time","/","",1,1,['samesite'=>'Lax']);
错误:PHP 警告:setcookie() 最多需要 7 个参数,其中 8 个给定 zzz.com/index.php 第 73 行,referer:https://zzz.com/
谢谢,
托德
PHP 7.3 引入了 setcookie() 的替代语法:
An alternative signature supporting an options array has been added. This signature supports also setting of the SameSite cookie attribute.
这意味着您只需像在旧版本中那样提供前两个参数,并将其余参数放在选项数组中:
setcookie('genone', 'genoneinfo', [
'expires' => $cookie_expiration_time,
'path' => '/',
'domain' => '',
'secure' => true,
'httponly' => true,
'samesite' =>'Lax',
]);
根据文档中的描述,旧版本的参数名称变成了数组键:
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.
PHP7.3 中 setcookie() 的正确语法是什么?我通常这样使用 setcookie():
setcookie("genone", "genoneinfo", "$cookie_expiration_time","/","",1,1);
可以,但是如何添加 samesite 选项?我试过这样,但失败并出现 php 错误:
setcookie("genone", "genoneinfo", "$cookie_expiration_time","/","",1,1,['samesite'=>'Lax']);
错误:PHP 警告:setcookie() 最多需要 7 个参数,其中 8 个给定 zzz.com/index.php 第 73 行,referer:https://zzz.com/
谢谢, 托德
PHP 7.3 引入了 setcookie() 的替代语法:
An alternative signature supporting an options array has been added. This signature supports also setting of the SameSite cookie attribute.
这意味着您只需像在旧版本中那样提供前两个参数,并将其余参数放在选项数组中:
setcookie('genone', 'genoneinfo', [
'expires' => $cookie_expiration_time,
'path' => '/',
'domain' => '',
'secure' => true,
'httponly' => true,
'samesite' =>'Lax',
]);
根据文档中的描述,旧版本的参数名称变成了数组键:
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.