Click here to Skip to main content
15,911,139 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
I'm trying to make cookies expire but it's not working:

I use

C#
function delete_Cookie( name, path, domain ) {
    if ( Get_Cookie( name ) )
        {
        document.cookie = name + "=" +
    ( ( path ) ? ";path=" + path : "") +
    ( ( domain ) ? ";domain=" + domain : "" ) +
    ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
        alert(name;);
        }
    else
        alert("none");
}


That alert alert(name); show the name of the cookie correctly, however it's not working, because I have:

XML
var id_sala = lerCookie('idsala');
document.write(" COOKIE lida idsala: " + lerCookie('idsala')+ "<br>");


And even if close the browser and go directly to the URL it still shows the cookie... I already commented the cookie where I set it (just to make sure). I do it with JSP:

/*Cookie cookie = new Cookie("idsala", Integer.toString(id)); 
cookie.setMaxAge(365 * 24 * 60 * 60);
response.addCookie(cookie);)*/


That delete function is associated with a html button:

C#
function logout()
{
    delete_Cookie("idsala", "/", "");
    ( Get_Cookie( 'idsala' ) ) ? alert( Get_Cookie('idsala')) :
    alert( "it is gone");
}



I have no idea why this is happening, could you help me ? The only solution (for now..) is deleting cookies with the browser settings.
Posted
Updated 14-Apr-11 6:33am
v2

1 solution

You cannot directly delete a cookie on a user's computer. However, you can direct the user's browser to delete the cookie by setting the cookie's expiration date to a past date. The next time a user makes a request to a page within the domain or path that set the cookie, the browser will determine that the cookie has expired and remove it.

if (Request.Cookies["UserSettings"] != null)
{
    HttpCookie myCookie = new HttpCookie("UserSettings");
    myCookie.Expires = DateTime.Now.AddDays(-1d);
    Response.Cookies.Add(myCookie);
}


http://msdn.microsoft.com/en-us/library/ms178195.aspx[^]
 
Share this answer
 
Comments
Maxdd 7 15-Apr-11 8:06am    
That's what I was trying to do with ";expires=Thu, 01-Jan-1970 00:00:01 GMT"; Thanks!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900