Results 1 to 3 of 3

Thread: Does cookie expire in time?

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Answers
    1

    Does cookie expire in time?

    In the following lines of code, a cookie is created and made to expire in 2 seconds. But when accessed the cookie after the expiration time the cookie still persists.

    HttpCookie httpCookie = new HttpCookie("EmpName", "James");

    //Cookie set to expire in 2 seconds
    httpCookie.Expires = DateTime.Now.AddSeconds(2);

    Response.Cookies.Add(httpCookie);
    //The process is made to wait 3 seconds
    Thread.Sleep(3000);

    HttpCookie myCookie= Request.Cookies["EmpName"];
    if (myCookie != null)
    {
    TextBox1.Text = "Cookie still persists";
    }
    else
    {
    TextBox1.Text = "Cookie has expired";
    }

    The output of the above code assigns string "Cookie still persists" to the textbox1 but as per my understanding the cookie is supposed to expire in 2 seconds and "Cookie has expired" string is to be assigned to the textbox1.

    What could be the reason for the above behavior of cookies?


  2. #2

    Re: Does cookie expire in time?

    When the browser checks a cokie and its expiration date is passed then the browser will delete the cookie.

    So the cookie will not be deleted untill the browser checks the cookie.

    The browser will check the cookie when you will make a request to a webpage related to the cookie.


  3. #3
    Junior Member
    Join Date
    Jan 2011
    Answers
    10

    Re: Does cookie expire in time?

    I am using Following code for Delete the cookie, You can also try this.

    setcookie("TestCookie", "blar", time()+3600); /* expire in 1 hour */

    I think this code will help you to delete cookies.


  4.  Sponsored Ads
    Ad


     

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
About us
Applying for a job can be a stressful and frustrating experience, especially for someone who has never done it before. Considering that you are competing for the position with a at least a dozen other applicants, it is imperative that you thoroughly prepare for the job interview, in order to stand a good chance of getting hired. That's where GeekInterview can help.
Interact