Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: delete_cookie() helper not working #4149

Closed
iszakur opened this issue Jan 23, 2021 · 7 comments
Closed

Bug: delete_cookie() helper not working #4149

iszakur opened this issue Jan 23, 2021 · 7 comments
Labels
bug Verified issues on the current code behavior or pull requests that will fix them

Comments

@iszakur
Copy link

iszakur commented Jan 23, 2021

Direction

Describe the bug
I tried use cookie, and I succeed to make it, but when I tried to delete session not working, I tried some method that I found in forum but still not working.

CodeIgniter 4 version
CI4 v.4.0.4 and CI4 development

Affected module(s)

Expected behavior, and steps to reproduce if appropriate
this code to set cookie
set_cookie('key1', $cek_user['id'], 3600);

this code to delete cookie I tried

setcookie('key1', '', time() - 3600);
delete_cookie('key1');
$this->response->deleteCookie('key1');
$this->response->setCookie(['name' => 'key1', 'value' => "", 'expire' => '- 3600']);
set_cookie('key1', '', time() - 3600);

Context

  • OS: Windows 10
  • Web server Apache/2.4.43 (Win64)
  • XAMPP Version 7.4.6
  • PHP version 7.4.6
@iszakur iszakur added the bug Verified issues on the current code behavior or pull requests that will fix them label Jan 23, 2021
@iRedds
Copy link
Collaborator

iRedds commented Jan 24, 2021

I cannot reproduce.
Even if you use the exit keyword to terminate the script, setcookie('key1', '', time() - 3600); will always set headers in response.
And if it doesn't work, then the problem is not in the framework.

@iszakur
Copy link
Author

iszakur commented Jan 24, 2021

I cannot reproduce.
Even if you use the exit keyword to terminate the script, setcookie('key1', '', time() - 3600); will always set headers in response.
And if it doesn't work, then the problem is not in the framework.

it's just one of method I used to delete cookie, I've been just used delete_cookie('key1'); but not working at all

@iRedds
Copy link
Collaborator

iRedds commented Jan 24, 2021

set_cookie('key', 'value', 'expire') equal setcookie('key', 'value', 'expire')
delete_cookie('key') equal setcookie('key', '', '')

set_cookie() and delete_cookie() work the same

set_cookie/delete_cookie -> Responce -> collecting data in array -> pass data from array to setcookie

If you can set cookies using the set_cookie() function, then there will be no problems with deleting.

@iszakur
Copy link
Author

iszakur commented Jan 24, 2021

you can set cookies using the set_cookie() function, then there will be no problems with

I've used set_cookie() and clear with delete_cookie() thousand times, but when I check with d(get_cookie('key1')); or check in tab application of delevopers tools in google chrome, the cookie is still available

@iRedds
Copy link
Collaborator

iRedds commented Jan 24, 2021

Check in DevTools in the Network tab which headers are returned
2021-01-24_15-56-54

@paulbalandan
Copy link
Member

Setting/deleting cookies are made in the Response while getting cookies are made in the Request. In order for you to get the cookie, you need to have the response send the cookie.

Read further: #3939

@iszakur
Copy link
Author

iszakur commented Jan 24, 2021

Setting/deleting cookies are made in the Response while getting cookies are made in the Request. In order for you to get the cookie, you need to have the response send the cookie.

Read further: #3939

finally I could delete the cookie, deleting cookie made in response header when using withCookies() function. I tried using that function when redirect to another page. this code I use :
redirect()->to(base_url('auth/login'))->withCookies();
and made it. thank you.... 💯

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Verified issues on the current code behavior or pull requests that will fix them
Projects
None yet
Development

No branches or pull requests

3 participants