简体   繁体   English

Setcookie 不设置 cookie

[英]Setcookie does not set a cookie

Hi guys im trying to set a cookie using大家好,我正在尝试使用

    setcookie("ms_il_cart_save_for_3", "cName", time()+3600);
    header("Location: store-cart3.php");
    exit;

when i am move to store-cart3.php the cookie was not set var dump on cookie shows NULL, this has work for me for a year by now.当我搬到 store-cart3.php 时,cookie 未设置 var dump on cookie 显示 NULL,这对我来说已经工作了一年。 today i have updated changes no related to this piece of code, i know for a fact this worked so far, nothing is outputed with HTML before this code and i dont think i changed the files encoding, maybe my web server blocks creating cookie because of security mesures?今天我更新了与这段代码无关的更改,我知道到目前为止这是有效的,在此代码之前 HTML 没有输出任何内容,我认为我没有更改文件编码,也许我的 web 服务器阻止创建 cookie,因为安全措施? (i have run this code today about 100 times) (我今天已经运行了这个代码大约 100 次)

this is pritty annoying any ideas?这很烦人有什么想法吗?

Cookie setting will only work if headers haven't been sent yet. Cookie 设置仅在尚未发送标头时才有效。 If you've already sent headers or content to the client then setcookie won't work.如果您已经向客户端发送了标头或内容,那么 setcookie 将不起作用。 Setting cookies also requires the client to accept the cookie, if it doesn't then there's nothing you can do about that other than inform them that they need to accept cookies for your system to work.设置 cookies 还需要客户端接受 cookie,如果没有,那么除了通知他们需要接受 cookies 以使您的系统正常工作之外,您无能为力。

EDIT: You said in your post that you made changes to unrelated code and now your setcookie doesn't work any more.编辑:您在帖子中说您对不相关的代码进行了更改,现在您的 setcookie 不再起作用。 It is possible that your unrelated code has an error in it that's causing PHP to echo an error message to the browser.您的不相关代码可能存在错误,导致 PHP 向浏览器回显错误消息。 This will cause all headers to be sent and any setcookie calls made after this point won't work.这将导致发送所有标头,并且在此之后进行的任何 setcookie 调用都将不起作用。

When you're setting a cookie on a page that redirects in this manner, you have to set the cookie after the call to header() .当您在以这种方式重定向的页面上设置 cookie 时,您必须在调用header()之后设置 cookie。

So your example needs to be:所以你的例子需要是:

header("Location: store-cart3.php");
setcookie("ms_il_cart_save_for_3", "cName", time()+3600);
exit;

Propably you have outpuded some content before calling that functions and headers were already sent.在调用函数和标头之前,您可能已经输出了一些内容。

Try to start output buffering at start of your script by ob_start()尝试通过ob_start()在脚本开始时启动 output 缓冲

Also check your files for warnings/notices and BOM utf-8 bytes at begining of text file, wich ones aren't seen in text editor.还要在文本文件的开头检查您的文件中的警告/通知和 BOM utf-8 字节,在文本编辑器中看不到这些字节。

Is it browser specific?它是特定于浏览器的吗? I've noticed that Blackberry browsers don't process cookies returned in a redirect response - but MSIE and Firefox are happy to.我注意到 Blackberry 浏览器不会处理在重定向响应中返回的 cookies - 但 MSIE 和 Firefox 很高兴。

Since the redirect is after the setcookie, I presume it would be fairly obvious if the headers had already been sent.由于重定向是在 setcookie 之后,我认为如果标头已经发送,那将是相当明显的。 But have you checked that the cookie is still in the response headers?但是您是否检查过 cookie 是否仍在响应标头中? (ieHTTPHeaders, firebug, tamperdata, fiddler, wireshark) (即HTTPHeaders、firebug、tamperdata、fiddler、wireshark)

If not, try switching around the order of the setcookie() and header() calls.如果没有,请尝试切换 setcookie() 和 header() 调用的顺序。

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

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