如何设置 cookie 然后在 PHP 中重定向?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/612034/
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
How can I set a cookie and then redirect in PHP?
提问by Wickethewok
After doing a bit of processing, I want to set a cookie value to user input and then redirect them to a new page. However, the cookie is not getting set. If I comment out the redirect, then the cookie is set successfully. I assume this is a header issue of some sort. What is the best workaround for this situation?
在做了一些处理之后,我想为用户输入设置一个 cookie 值,然后将它们重定向到一个新页面。但是,cookie 没有被设置。如果我注释掉重定向,则 cookie 设置成功。我认为这是某种标题问题。这种情况的最佳解决方法是什么?
if($form_submitted) {
...
setcookie('type_id', $new_type_id, time() + 60*60*24*30);
header("Location: $url");
exit;
}
Note that setcookie returns truein either case and I get no errors/warnings/notices.
请注意,true无论哪种情况,setcookie 都会返回,并且我没有收到任何错误/警告/通知。
EDIT:I am using Unix/Apache/MySQL/PHP
编辑:我正在使用 Unix/Apache/MySQL/PHP
回答by Glavi?
If you have human urls or subfolders (like www.domain.com/path1/path2/), then you must set cookie path to / to work for all paths, not just current one.
如果您有人工 url 或子文件夹(如 www.domain.com/path1/path2/),那么您必须将 cookie 路径设置为 / 以适用于所有路径,而不仅仅是当前路径。
if($form_submitted) {
...
setcookie('type_id', $new_type_id, time() + 60*60*24*30, '/');
header("Location: $url");
exit;
}
From PHP manual:
来自 PHP 手册:
The path on the server in which the cookie will be available on. If set to '/', the cookie will be available within the entire domain . If set to '/foo/', the cookie will only be available within the /foo/ directory and all sub-directories such as /foo/bar/ of domain . The default value is the current directory that the cookie is being set in.
cookie 可用的服务器上的路径。如果设置为“/”,cookie 将在整个域中可用。如果设置为 '/foo/',则 cookie 将仅在 /foo/ 目录和域的所有子目录(例如 /foo/bar/ )中可用。默认值是设置 cookie 的当前目录。
回答by Daniel Von Fange
I'm assuming you are running IIS? There is a know bug with IIS versions less than 7 when attempting to both set a cookie and a location header in the same request.
我假设您正在运行 IIS?当尝试在同一请求中设置 cookie 和位置标头时,IIS 版本低于 7 存在一个已知错误。
回答by tkotitan
How are you testing if the cookie is set? Cookies are available on the nextpage after they are set.
你如何测试cookie是否设置?Cookie 设置后可在下一页使用。
Common Pitfalls:
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);.
常见的陷阱:
直到下一次加载 cookie 应该可见的页面时,cookie 才会变得可见。要测试 cookie 是否已成功设置,请在 cookie 过期之前在下一个加载页面上检查 cookie。过期时间通过 expire 参数设置。调试 cookie 存在的一个好方法是简单地调用 print_r($_COOKIE);。
回答by atonyc
I was able to solve this problem by using a slight delay in the refresh header. We set the header (which must be done before any methods which might output, like setcookie), and then set the cookies. I added a message so that the user doesn't see a blank screen for those couple seconds.
我能够通过在刷新标头中使用轻微延迟来解决这个问题。我们设置标题(必须在任何可能输出的方法之前完成,例如 setcookie),然后设置 cookie。我添加了一条消息,以便用户在这几秒钟内不会看到空白屏幕。
header("refresh: 2; url=$url");
setcookie('type_id', $new_type_id, time() + 60*60*24*30, '/');
echo "Processing, please wait...";
回答by Ondrej Konrady
Use a relative URL in the header:
在标题中使用相对 URL:
@Header("Location: orders_9090_1.php");

