[英]Set a cookie without the`domain`, but with `secure` and `httponly`
PHP's setcookie
function looks like this: PHP的
setcookie
函数如下所示:
bool setcookie ( string $name [, string $value [, int $expire = 0 [, string $path [, string $domain [, bool $secure = false [, bool $httponly = false ]]]]]] )
As you can see, domain
has no standard value, but the following parameters ( httponly
and secure
) do. 如您所见,
domain
没有标准值,但是以下参数( httponly
和secure
) httponly
。
I want both last parameters set to true, but without specifying a domain to bind the cookie to. 我希望最后两个参数都设置为true,但不指定将cookie绑定到的域。 Is that even possible?
那有可能吗? Sadly, PHP doesn't allow something like this:
可悲的是,PHP不允许这样的事情:
setcookie('name', 'value', time()+60, '/', $secure=true, $httponly=true);
Also see this SO question with a similar problem . 另请参阅带有类似问题的SO问题 。 Obviously, I cannot change the
setcookie
function. 显然,我无法更改
setcookie
函数。
EDIT 编辑
Setting the domain
parameter to ""
or null
does not work. 将
domain
参数设置为""
或null
。
Testing this on my own development server (PHP 7.3.4), submitting an empty string for the domain works. 在我自己的开发服务器(PHP 7.3.4)上进行测试,为该域提交一个空字符串。 Submitting null may work as well, but with declare(strict_types=1) set, PHP generates a message about an incorrect parameter type, string required, NULL supplied.
提交null也可以,但是设置了define(strict_types = 1)后 ,PHP会生成一条消息,提示有关参数类型错误,所需字符串,提供了NULL。
For example: 例如:
setcookie('myCookie', 'myCookieData', 0, '/, '', true, true);
but not 但不是
setcookie('myCookie', 'myCookieData', 0, '/, null, true, true);
However, setcookie() allows cookie options to be supplied as an array. 但是, setcookie()允许将cookie选项作为数组提供。
For example: 例如:
setcookie('myCookie', 'myCookieData',['expires'=>0, 'path'=>'/', 'domain'=>'example.com', 'httponly'=>true, 'secure'=>true];
This allows the domain to be omitted from the array, and it's thus not sent to the client: 这允许将域从数组中省略,因此不会将其发送给客户端:
setcookie('myCookie', 'myCookieData',['expires'=>0, 'path'=>'/', 'httponly'=>true, 'secure'=>true];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.