查询PHP cookie [英] Query about PHP cookie
问题描述
我是PHP新手,正在使用PHP cookie,但遇到了无法正确设置cookie的问题。
这是Set Cookie的语句
I'm new PHP guy and I'm using PHP cookie and I'm facing a problem that the cookie can not be set correctly. Here is the statement of Set Cookie
setcookie('cookieusername',$ username,100000);
setcookie('cookieusername', $username, 100000);
和获取Cookie的语句
and the statement of Get Cookie
$ cookieusername = $ _COOKIE [ cookieusername];
$cookieusername = $_COOKIE["cookieusername"];
问题是, $ _ COOKIE [ cookieusername]的值;
未定义。
我不知道问题是什么。我已经厌倦了将cookie路径设置为 /,但是仍然无法使用。
I don't know what the problem is. I have tired to set the cookie path to '/' but which was still not work.
推荐答案
而不是:
setcookie('cookieusername', $username, 100000);
您必须做的事情:
setcookie('cookieusername', $username, time() + 100000);
原因是第三个参数是到期时间(作为Unix时间戳(自时期)),而不是到期之前的时间。而这是手册的链接。
The reason is that the third parameter is the expiry time (as a Unix timestamp (number of seconds since the epoch)), not the time until expiry. And here's the link of manual.
这篇关于查询PHP cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!