在重定向之前设置Cookie [英] Setting a cookie before a redirect

查看:98
本文介绍了在重定向之前设置Cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图在标题重定向之前设置cookie,但是它不起作用.我已经读过,在标题重定向行之后设置cookie应该可以解决问题,但是我没有运气.同样,此处的一篇帖子以前建议,如果您在位置重定向中使用的是人工URL",则应在Cookie的path参数中使用"/".这也没有运气.

I'm attempting to set a cookie just before a header redirect but it is not working. I have read that setting the cookie after the header redirect line should solve the problem, but I am having no luck. Similarly, a post here previously suggested that if you were using a 'human URL' in the location redirect, you should use '/' in the path parameter of the cookie. This has also been done with no luck.

header("Location: $url" . $params);
setcookie('cartstlang', 'lang', 0, '/', '', FALSE, FALSE);
setcookie('cartstdb', 'db', 0, '/', '', FALSE, FALSE);

另外请注意,我已经通过注释掉标题重定向,然后单击链接来更改页面来进行测试.然后,这些cookie在var_dump()上看起来还不错.

Also note, I have tested this by commenting out the header redirect and then clicking on an link to change pages. The cookies then appeared fine on a var_dump().

我正在使用PHP 5.3+在XAMPP上进行开发.我已经在IE 8和Firefox 4中进行了测试.

I am developing on XAMPP with PHP 5.3+. I have tested in both IE 8 and Firefox 4.

任何帮助都会很棒.谢谢.

Any help would be great. Thanks.

推荐答案

setcookie 代码应位于 header 代码之前,并确保您执行 exit()之后停止任何输出.

The setcookie code should come before the header code and make sure you do an exit() after to stop any output.

您还确定它将去往同一个域( http://site.com http://www.site.com )?尝试将 setcookie 中的domain选项设置为 .site.com ,以便可以在所有子域中访问它.

Also are you sure it's going to the same domain (there is a difference between http://site.com and http://www.site.com)? Try setting the domain option to .site.com in the setcookie so it can be accessed across all subdomains.

这篇关于在重定向之前设置Cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆