1

It's kind of weird, but I set a cookie with:

setcookie('auth', 'ok', time() + 3600, "/");

Result:

Name:   auth
Content:    ok
Domain: --.com
Path:   /
Send for:   Any kind of connection
Accessible to script:   Yes
Created:    Tuesday, July 11, 2017 at 11:00:23 PM
Expires:    Wednesday, July 12, 2017 at 12:00:23 AM

It works fine, but when I want to remove this cookie:

setcookie('auth', 'ok', time() - 3600, "/");
unset($_COOKIE['auth']);

there is no change in the result, and the cookie is not removed, and I still got this cookie in my browser.

The cookie is created in this URL:

auth.php?action=confirm

and call a function with ajax:

model/auth.php

And it should remove it in this URL:

auth.php?action=logout

But it is not working. Any idea?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Pedram
  • 15,766
  • 10
  • 44
  • 73

3 Answers3

0

A clean way to delete a cookie is to clear both of the $_COOKIE value and browser cookie file:

if (isset($_COOKIE['auth'])) {
  unset($_COOKIE['auth']);
  setcookie('auth', '', time() - 3600, '/'); // Empty value and old timestamp
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Harpreet Singh
  • 999
  • 9
  • 19
0

Most likely you are outputting some data before destroying the cookie.

Try to check this, and before any output use this code:

setcookie("auth", "", time()-3600);

With this $sent = headers_sent($file, $line); you can see if some data have been outputted before you tried to destroy the cookie.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
JTC
  • 3,344
  • 3
  • 28
  • 46
0

You have to check if the path is also correct. You can do this by opening microsoft edge -> settings -> cookies and site permmision -> see all cookies -> search for the domain name of your site -> click the drop down and find the cookie name -> view the imformation. Under the information you will find path. The path is not always "/"

Then just replace "/" with the path you find there. example if the path is "/abc"

setcookie('auth', null, time() - 3600, "/abc");

You might be able to find cookie info on other browsers but I only found how to do it on microsoft edge.

cigien
  • 57,834
  • 11
  • 73
  • 112
Adebisi
  • 11
  • 2