简体   繁体   English

PHP Cookie将到期时间设置为无

[英]PHP Cookie set expiry time to none

I am setting a cookie, but having some issue. 我正在设置一个cookie,但有一些问题。 Currently the cookie format is this: 目前cookie格式是这样的:

setcookie("company_id", $company_id, 0, '/');

So expiry time is set to 0 means cookie will never expire, but when I tried to not to set any expiry time so I passes an empty argument in the third parameter like this: 所以到期时间设置为0表示cookie永远不会过期,但是当我试图不设置任何到期时间时,我在第三个参数中传递一个空参数,如下所示:

setcookie("company_id", $_POST["company_id"], "", '/');

But this is not working, cookie is not getting, when I changed the empty argument to 0 it than start working. 但这不起作用,cookie没有得到,当我将空参数更改为0它比开始工作。 Any suggestions? 有什么建议么?

The expire parameter is in seconds. expire参数以秒为单位。 Setcookie function Setcookie功能

All the arguments except the name argument are optional. 除name参数之外的所有参数都是可选的。 You may also replace an argument with an empty string ("") in order to skip that argument. 您也可以用空字符串(“”)替换参数以跳过该参数。 Because the expire argument is integer, it cannot be skipped with an empty string, use a zero (0) instead. 因为expire参数是整数,所以不能使用空字符串跳过它,而是使用零(0)。

So unless you want it to expire at the end of the session you will need to have a value other than zero. 因此,除非您希望它在会话结束时到期,否则您将需要具有零以外的值。

0 suggests the cookie should expire at the end of the session, if you want it to 'never expire'use a really long time. 0表示如果你想让它“永不过期”使用很​​长时间,那么cookie应该在会话结束时到期。

setcookie("TestCookie", $value, time()+3600*24*365*10, '/')

Eg that would be ten years. 例如那将是十年。

Bear in mind people can clear cookies often. 请记住,人们可以经常清除cookie。 So yours may get deleted. 所以你的可能会被删除。 Cookies are also browser specific, so if the data that is stored in this, is needed every-time, then if the user switches browser this will no longer be available unless you set it again. Cookie也是特定于浏览器的,因此如果每次都需要存储在此中的数据,那么如果用户切换浏览器,除非再次设置,否则这将不再可用。

You're asking the wrong question. 你问的是错误的问题。 There is no way to set a cookie to not expire; 没有办法设置cookie不会过期; This is not a PHP limitation, it's just not part of the specification for cookies (http://www.faqs.org/rfcs/rfc2965.html). 这不是PHP限制,它不是cookie规范的一部分(http://www.faqs.org/rfcs/rfc2965.html)。

Your best bet is to use a date far in the future. 你最好的选择是在将来使用一个日期。 For example, to set a cookie that expires in twenty years: 例如,要设置一个在二十年后到期的cookie:

setcookie(
  "CookieName",
  "CookieValue",
  time() + (20 * 365 * 24 * 60 * 60)
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM