0

Can you please tell me why both the PHP and Javascript versions can't delete the cookie target of the script named "t"?

(no httponly, and has been created via js btw)

<?php
    include('functions.php');
    sec_session_start();
    $lang = check_lang();
    include("../config/lang/".$lang.".php");

    // Unset all session values
    $_SESSION = array();

    // get session parameters 
    $params = session_get_cookie_params();

    // Delete the actual cookie.
    setcookie(session_name(), '', time() - 42000, $params["path"], $params["domain"], $params["secure"], $params["httponly"]);
    setcookie('t',"",-3600); //<-- this one doesn't work)

    // Destroy session
    session_destroy();

    echo "<script language='javascript'>
        function del_cookie(name) {
            document.cookie = name + '=; expires=Thu, 01-Jan-70 00:00:01 GMT;';
        } 
        del_cookie('t'); //<-- this one neither.
        </script>
        <h1>$l[logout_ok]</h1>";
?>
Mike
  • 2,132
  • 3
  • 20
  • 33
lyllo
  • 45
  • 7

1 Answers1

0

I point you to http://ca3.php.net/manual/en/function.setcookie.php, specifically the part that says: "expire: The time the cookie expires. This is a Unix timestamp so is in number of seconds since the epoch." so you can't give it a negative value, as well as the part that says: "If set to 0, or omitted, the cookie will expire at the end of the session."

Just use setcookie("t", "", false), and done. The cookie is now unset for the next request made by the browser to the server (be that as real pageload, or an ajax request). That said, cookies are only relevant during the HTTP request phase, you shouldn't be using them other than to set values that the server needs to know about across multiple requests. Don't use them for things that happen on the page (and consequently, don't try to manipulate them through JavaScript, that's not where they're relevant).

Mike 'Pomax' Kamermans
  • 49,297
  • 16
  • 112
  • 153
  • _"The cookie is now unset"_ when you ask the user nicely to close his browser, and if he's using Firefox, ask him nicely to [to disable his restore feature](http://stackoverflow.com/questions/777767/firefox-session-cookies) – Wrikken May 15 '13 at 01:27