delete cookie in php

前端 未结 5 855
心在旅途
心在旅途 2021-01-28 06:01

I am trying to delete a cookie.

I am using setcookie(\"PHPSESSID\", \"\", time() - 6400); which deletes the cookie just fine.

However it is not enti

相关标签:
5条回答
  • 2021-01-28 06:05

    I had such problem for my logout code, after hard work and researches I myself finally figured it out and used javascript to solve the problem.

    You can easily do that in client-side using script below, you might need to change value of path and host:

    document.cookie = "PHPSESSID=; expires=Thu, 01 Jan 1970 00:00:00   UTC;path=/;host=localhost";
    
    0 讨论(0)
  • 2021-01-28 06:05

    This code can solve this problem:

    session_start(); // initialize session
    session_destroy(); // destroy session
    setcookie("PHPSESSID","",time()-3600,"/"); // delete session cookie
    
    0 讨论(0)
  • 2021-01-28 06:13

    using setcookie("PHPSESSID", "", time() - 6400); expires the cookie like 2 hours ago, try using setcookie("PHPSESSID", "", 1); to expire it at epoch January 1st, 1970.

    if that doesn't work you can try adding in the path like this setcookie("PHPSESSID","",time()-6400,"/");

    You can try this example from http://www.php.net/manual/en/function.setcookie.php#73484 to remove all cookies, but I'm since this seems to be some sort of supercookie who knows..

    // unset cookies
    if (isset($_SERVER['HTTP_COOKIE'])) {
        $cookies = explode(';', $_SERVER['HTTP_COOKIE']);
        foreach($cookies as $cookie) {
            $parts = explode('=', $cookie);
            $name = trim($parts[0]);
            setcookie($name, '', time()-1000);
            setcookie($name, '', time()-1000, '/');
        }
    }
    
    0 讨论(0)
  • 2021-01-28 06:13

    See Example 1 here to delete and destroy a session:

    http://php.net/manual/en/function.session-destroy.php

    first unset the cookie, then destroy the session.

    0 讨论(0)
  • 2021-01-28 06:20

    You might want to unset the $_COOKIE variable too, by adding a

    unset($_COOKIE['PHPSESSID']);
    

    in the next line. That however just affects the currently loaded page.

    0 讨论(0)
提交回复
热议问题