开发者

problem with cookies

i have a page that create a cookie value using the set_cookie like this:

setcookie("is_voted","1",time()+60*60*24*10,"/~poll/","spacepower.tv");

but i can't get this cookie value using the $_COOKIE arr开发者_如何学编程ay

how to fix this problem ?


I've encountered the same problem. According to the PHP manual:

Cookies will not become visible until the next loading of a page that the cookie should be visible for. To test if a cookie was successfully set, check for the cookie on a next loading page before the cookie expires. Expire time is set via the expire parameter. A nice way to debug the existence of cookies is by simply calling print_r($_COOKIE);.

Basically, the $_COOKIE superglobal is only updated when the script first runs. If you set a new cookie, it won't be immediately available in $_COOKIES. You have to start a script to see updated $_COOKIE values. If you don't see them then, you have a problem.


According to php.net:

Cookies are part of the HTTP header, so setcookie() must be called before any output is sent to the browser.

When they say before any output, they mean any output. If there is any whitespace, including blank lines, outside of the <?php ?> blocks, it will cause the headers to be sent and the cookie will not be set.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜