V4n1ll4
V4n1ll4

Reputation: 6099

Cookie::forget not working laravel 5.1

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.

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

Have I done something wrong?

Upvotes: 17

Views: 32726

Answers (9)

jeff-h
jeff-h

Reputation: 2624

Your code is perfect, so there's some other issue.

Cookies are tricky little ^##$$ and to make things worse are dependent on the client's implementation; various browsers may well handle cookie edge cases differently, or may even have had long-standing bugs relating to their cookie handling.

The "removal" of a cookie actually involves sending an update to the cookie but with an expiration date in the past. From RFC 6265:

Finally, to remove a cookie, the server returns a Set-Cookie header with an expiration date in the past. The server will be successful in removing the cookie only if the Path and the Domain attribute in the Set-Cookie header match the values used when the cookie was created.

If your Laravel code looks fine, as in the original question, I would suggest inspecting your cookies in your browser's dev tools. For example, Chrome's "Network" tab has a "Cookies" tab which shows you the Request Cookies and the Response Cookies. You may find there is a subtle difference between the original cookie and the cookie being sent to unset it. As per the RFC above, a difference in the domain (even just a leading dot) will break the cookie removal.

Upvotes: 0

Sumit Kumar Gupta
Sumit Kumar Gupta

Reputation: 2364

First, make sure you've imported Cookie class with use keyword, like below :

use Cookie;

Next, create a function and delete a cookie by name

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

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

Upvotes: 3

Emmanuel David
Emmanuel David

Reputation: 411

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

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.

Upvotes: 0

Bond77
Bond77

Reputation: 176

You can also do it this way:

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

Upvotes: 0

Fillie
Fillie

Reputation: 223

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).

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.

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

Upvotes: 0

Davinder Singh
Davinder Singh

Reputation: 1

public function funname(CookieJar $cookie)

    session()->flush();

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

return redirect('/');

Upvotes: 0

Gediminas Šukys
Gediminas Šukys

Reputation: 7391

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

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

Upvotes: 6

Robin van Baalen
Robin van Baalen

Reputation: 3651

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.

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::queue(
    Cookie::forget('cookieName')
);

The CookieJar will pass all queued cookies to the next response.

Upvotes: 22

Jan.J
Jan.J

Reputation: 3080

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

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

Upvotes: 30

Related Questions