Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cookie expiration date

Tags:

I am not a programmer. I am trying to use a cookie script that remembers the last drop down menu selection.

I found a script that works but it does only a session cookie. How do I add an expiration date to the cookie in this script?

<head>
  <script>        
    function SETcookie() {
      document.cookie = "Selected=" + document.getElementById('myList').selectedIndex;
    }

    function GETcookie() {
      if (document.cookie) {
        eval(document.cookie);
        document.getElementById('myList').selectedIndex = Selected;
      }
    }    
  </script>
</head>

<body onLoad="GETcookie()">
  <select id="myList" onChange="SETcookie()">
    <option value="1">Option 1</option>
    <option value="2">Option 2</option>
    <option value="3">Option 3</option>
    <option value="4">Option 4</option>
  </select>
</body>
like image 972
mr.data1 Avatar asked Dec 01 '11 09:12

mr.data1


People also ask

Do cookies have an expiration date?

COOKIES, COMMERCIALLY PACKAGED - UNOPENEDProperly stored, a package of unopened cookies will generally stay at best quality for about 6 to 9 months.

Can you eat expired cookies?

Cookies, Crackers and Chips When cookies or chips get old, the stale taste is quite obvious. But as long as it doesn't smell funky (the oils in the cookie may go bad over a long period of time) and it doesn't crumble apart in your hand, then it's okay to eat.

What is the expiration time in cookies?

Cookies typically expire somewhere between 30 and 90 days. Though, some companies do implement shorter cookie expiration dates. It's also important to note that cookies can be deleted manually by the user or through third-party security programs. The cookie expiration time is very important.

What is the default expiration date of cookie?

What is the timeout of Cookie? The default time for a Cookie to expire is 30 minutes. The default Expires value for a cookie is not a static time, but it creates a Session cookie. This will stay active until the user closes their browser/clears their cookies.


1 Answers

Try this:

function setCookie(c_name,c_value,exdays) {
   var exdate=new Date();
   exdate.setDate(exdate.getDate() + exdays);
   document.cookie=encodeURIComponent(c_name) 
     + "=" + encodeURIComponent(c_value)
     + (!exdays ? "" : "; expires="+exdate.toUTCString());
     ;
}

c_name is the name of the cookie

c_value is the cookie value

exdays is the number of days you want the cookie to expire after

Source: http://www.w3schools.com/js/js_cookies.asp

like image 147
vimist Avatar answered Sep 28 '22 08:09

vimist