php Cookie::忘记不工作laravel 5.1

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31941672/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:44:03  来源:igfitidea点击:

Cookie::forget not working laravel 5.1

phplaravellaravel-5.1

提问by V4n1ll4

I'm trying to get Laravel 5.1 to delete my cookie, however it will not delete even though i'm returning it with my redirect.

我正在尝试让 Laravel 5.1 删除我的 cookie,但是即使我通过重定向返回它,它也不会删除。

return redirect('/voucher')->withCookie(Cookie::forget($cookie));

Have I done something wrong?

我做错了什么吗?

回答by Jan.J

Maybe I am wrong, but you are probably using cookie object in place of cookie name when calling Cookie::forget($cookie). Unless $cookieis a string containing cookie name, you should try something like this:

也许我错了,但您可能在调用Cookie::forget($cookie). 除非$cookie是包含 cookie 名称的字符串,否则您应该尝试以下操作:

return redirect('/voucher')->withCookie(Cookie::forget('cookie_name'));

回答by Robin van Baalen

I know this is already an old and answered question but I got here recently and if I'm correct, the cookie needs to be 'queued' for the next response.

我知道这已经是一个老问题,但我最近来到这里,如果我是对的,cookie 需要“排队”等待下一个响应。

You can do that by adding the cookie manually to the response as @Jan.J already described in his answer. But if you need to do it inline, this might also work for you:

您可以通过将 cookie 手动添加到响应中来做到这一点,正如@Jan.J 在他的回答中已经描述的那样。但是,如果您需要内联进行,这也可能对您有用:

Cookie::queue(
    Cookie::forget('cookieName')
);

The CookieJarwill pass all queued cookies to the next response.

CookieJar会通过所有排队的cookies到下一个响应。

回答by Gediminas

In my case there was an arraystored in the cookie, so none of provided methods has worked. Array should be deleted providing exactly pair of array:

在我的情况下array,cookie 中存储了一个,所以提供的方法都没有工作。应该删除数组,提供精确的数组对:

Cookie::queue(Cookie::forget('array_name[provide_key]'));

回答by Sumit Kumar Gupta

First, make sure you've imported Cookie classwith usekeyword, like below :

首先,确保您已Cookie class使用use关键字导入,如下所示:

use Cookie;

Next, create a function and delete a cookie by name

接下来,创建一个函数并按名称删除一个cookie

Cookie::queue(
  Cookie::forget('cookie_name_first')
);

Cookie::queue(
  Cookie::forget('cookie_name_second')
);

回答by Davinder Singh

public function funname(CookieJar $cookie)

公共函数 funname(CookieJar $cookie)

    session()->flush();

$cookie->queue(cookie()->forget('user_email')); $cookie->queue(cookie()->forget('user_password'));

$cookie->queue(cookie()->forget('user_email')); $cookie->queue(cookie()->forget('user_password'));

return redirect('/');

回答by Bond77

You can also do it this way:

你也可以这样做:

redirect('/')->cookie(cookie()->forget('my_super_cookie_name'));

回答by Fillie

Unfortunately none of the above worked for me, I'm not sure if it's a specific issue with this version of Laravel (5.1).

不幸的是,以上都不适合我,我不确定这是否是 Laravel (5.1) 版本的特定问题。

I did manage to get it working using raw PHP instead, by overwriting the existing cookie with an already expired one, I also had to specify a path to get this working. It's not as elegant as using a facade however.

我确实设法使用原始 PHP 使其工作,通过用已经过期的 cookie 覆盖现有 cookie,我还必须指定一个路径来使其工作。然而,它不像使用外观那么优雅。

setcookie('COOKIE_NAME', time() - 3600, '/');

回答by Emmanuel David

Recently I faced this issue while still on localhost but the issue was that I have written some code which weren't normal in my process of trying to overwrite the session config file. Therefore The default laravel

最近我在 localhost 上遇到了这个问题,但问题是我写了一些在尝试覆盖会话配置文件的过程中不正常的代码。因此默认的laravel

Cookie::queue(
   Cookie::forget('name')
      ) ;

Should work perfectly fine if you have not done any changes on your session.php config file. Check it out and you should be good to go. If you have made some changes try to ensure that your code conforms to standard and everything should work fine.

如果您没有对 session.php 配置文件进行任何更改,应该可以正常工作。检查一下,你应该很高兴。如果您进行了一些更改,请尝试确保您的代码符合标准并且一切正常。