PHP setcookie 1 年不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20833983/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP setcookie for 1 Year will not work
提问by jmenezes
I'm trying to set a PHP cookie to expire 1 Year from when the user logged in, and I'm doing it this way:
我正在尝试将 PHP cookie 设置为在用户登录后的 1 年内过期,我是这样做的:
setcookie("myCookie",'exampleUserName',(365 * 24 * 60) ,'/');
The problem is, when I view the cookie using the console in Chrome or Firefox, it show Expires Sun, 05, 2014in Chrome and Expires = Sessionin Firefox.
问题是,当我在 Chrome 或 Firefox 中使用控制台查看 cookie 时,它Sun, 05, 2014在 Chrome 和Expires = SessionFirefox 中显示过期。
Any other site like Google or stackoverflows cookies show the correct expiration date.
任何其他网站(如 Google 或 stackoverflows cookie)都会显示正确的到期日期。
How can I set this right?
我该如何正确设置?
回答by Shawn Rebelo
setcookie($cookie_name, $cookie_value, strtotime("+1 year"));
回答by Shankar Damodaran
Do like this...
这样做...
setcookie("myCookie",'exampleUserName',time()+31556926 ,'/');// where 31556926 is total seconds for a year.
回答by Jerry Pham
The third param is the time in future that mean time() + time in seconds. Your 365 * 24 * 60means 1971 ( 1970 + 1 ), this is in the past. Cookie in past will not be used - people set cookie to the past time to clear cookie.
Do exactly like Shankar.
第三个参数是表示未来的时间time() + time in seconds。你的365 * 24 * 60意思是 1971 ( 1970 + 1 ),这是过去。过去的 cookie 将不会被使用 - 人们将 cookie 设置为过去的时间以清除 cookie。完全像香卡一样。
回答by Anand Solanki
Try this:
尝试这个:
<?php
setcookie("TestName", "Test Value", time()+3600 * 24 * 365);
?>
>> Here 'TestName' is name of cookie.
>> "Test Value" is value to store.
>> time()+3600 * 24 * 365 - will set cookie time till 1 year.
Thanks!
谢谢!
回答by Marverick
try this one
试试这个
setcookie($cookie_name, $cookie_value, time() + ( 365 * 24 * 60 * 60));

