简体   繁体   English

如何设置 cookie,然后在 PHP 中重定向?

[英]How can I set a cookie and then redirect in PHP?

After doing a bit of processing, I want to set a cookie value to user input and then redirect them to a new page.经过一些处理后,我想为用户输入设置一个 cookie 值,然后将它们重定向到一个新页面。 However, the cookie is not getting set.但是,cookie 没有被设置。 If I comment out the redirect, then the cookie is set successfully.如果我注释掉重定向,则 cookie 设置成功。 I assume this is a header issue of some sort.我认为这是某种 header 问题。 What is the best workaround for this situation?这种情况的最佳解决方法是什么?

if($form_submitted) {
    ...
    setcookie('type_id', $new_type_id, time() + 60*60*24*30);
    header("Location: $url");
    exit;
}

Note that setcookie returns true in either case and I get no errors/warnings/notices.请注意,无论哪种情况,setcookie 都会返回true ,并且我没有收到任何错误/警告/通知。

EDIT: I am using Unix/Apache/MySQL/PHP编辑:我正在使用 Unix/Apache/MySQL/PHP

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. cookie 可用的服务器上的路径。 If set to '/', the cookie will be available within the entire domain.如果设置为“/”,cookie 将在整个域中可用。 If set to '/foo/', the cookie will only be available within the /foo/ directory and all sub-directories such as /foo/bar/ of domain.如果设置为 '/foo/',cookie 将仅在 /foo/ 目录和域的 /foo/bar/ 等所有子目录中可用。 The default value is the current directory that the cookie is being set in.默认值是设置 cookie 的当前目录。

How are you testing if the cookie is set?您如何测试 cookie 是否已设置? Cookies are available on the next page after they are set. Cookies 设置后在下一页可用。

Common Pitfalls:常见的陷阱:

Cookies will not become visible until the next loading of a page that the cookie should be visible for. Cookies 在下一次加载 cookie 应该可见的页面之前不会变为可见。 To test if a cookie was successfully set, check for the cookie on a next loading page before the cookie expires.要测试 cookie 是否设置成功,请在 cookie 过期之前检查下一个加载页面上的 cookie。 Expire time is set via the expire parameter.过期时间通过 expire 参数设置。 A nice way to debug the existence of cookies is by simply calling print_r($_COOKIE);.调试 cookies 存在的一个好方法是简单地调用 print_r($_COOKIE);。

I'm assuming you are running IIS?我假设您正在运行 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.尝试在同一请求中同时设置 cookie 和位置 header 时,IIS 版本小于 7 存在一个已知错误。

http://support.microsoft.com/kb/q176113/ http://support.microsoft.com/kb/q176113/

I was able to solve this problem by using a slight delay in the refresh header.我能够通过在刷新 header 中使用轻微延迟来解决这个问题。 We set the header (which must be done before any methods which might output, like setcookie), and then set the cookies.我们设置 header(必须在任何可能 output 的方法之前完成,如 setcookie),然后设置 cookies。 I added a message so that the user doesn't see a blank screen for those couple seconds.我添加了一条消息,以便用户在那几秒钟内看不到空白屏幕。

    header("refresh: 2; url=$url");
    setcookie('type_id', $new_type_id, time() + 60*60*24*30, '/');
    echo "Processing, please wait...";

Use a relative URL in the header:在 header 中使用相对的 URL:

@Header("Location: orders_9090_1.php");

I have made solution for that我已经为此制定了解决方案

    setcookie(self::SESSION_NAME, $session_id, $expires, '/', null, null, true);
    header("Set-cookie: ".self::SESSION_NAME."=".$session_id."; expires=".date('D, Y-M-d H:i:s', $expires)." GMT; path=/; HttpOnly; secure=true; SameSite=Strict");
    
//check is something printed
    if (!headers_sent()) {
    
//is there any header set
                if( !array_map(function($header){ return preg_match('/Set-cookie/i', $header) ? true : null; }, headers_list()) ){
    
                    die(header('Location: ' . $url));
                }
            }
            die('<script type="text/javascript">window.location.href = ' . ($append ? 'window.location.href' : '') . ($append && $url ? '+' : '') . ($url ? '"' . $url . '"' : '') . ';</script>');

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

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