Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I set a cookie expiration?

Status
Not open for further replies.

nalbiso

Programmer
Aug 31, 2000
71
US
I am a newbie at this and I need some help. I have created a form that stores the values that a user inputs through a cookie. Everytime I go to another page and then return to the form, the values that the user has input are there like they're supposed to be. But, when I shut off the browser and restart it and go back to that form, the values do not reappear.

How do I set the cookie so that it doesn't expire when the browser is shut off? See code:

function Get_Cookie(name) {
var start = document.cookie.indexOf(name+"=");
var len = start+name.length+1;
if ((!start) && (name != document.cookie.substring(0,name.length))) return null;
if (start == -1) return null;
var end = document.cookie.indexOf(";",len);
if (end == -1) end = document.cookie.length;
return unescape(document.cookie.substring(len,end));
}

function Set_Cookie(name,value,expires,path,domain,secure) {
document.cookie = name + "=" +escape(value) +
( (expires) ? ";expires=" + expires.toGMTString() : "") +
( (path) ? ";path=" + path : "") +
( (domain) ? ";domain=" + domain : "") +
( (secure) ? ";secure" : "");
}


Any help is appreciated :) [sig][/sig]
 
Hi, this will make it expire 30 days in the future:

var expires=new Date();
expires.setTime (expires.getTime() + 24 *60 *60 *30 * 1000);

Just add the number of milliseconds in whatever time period you want to expires.getTime(). To expire it in a year:

expires.setTime (expires.getTime() + 24 *60 *60 *365 * 1000);

Then you could embed that in your final string. Of course, this obviates the need for an 'expires' parameter to the function, unless you want to require the user of the function to do something like the above.

Also, there's a flaw in your Get_Cookie() function. Try setting cookies with the names "name" and "me" in that order with whatever values you want. You'll find that the Get_Cookie() function will return the value of the 'name' cookie instead of the value of the 'me' cookie when you call Get_Cookie("me"). The script also doesn't allow for encoded names (the cookie name isn't escaped before comparison).

Below's a function that doesn't have these problems (though it may have its own unique set of problems) ;)

function get_cookie(myname) {
var start,end,last_cookie=false,wc;

for (start=0;!last_cookie;start=end+2) {
if ((end=document.cookie.indexOf(";",start))==-1) {
end=document.cookie.length;
last_cookie=true;
}
wc=unescape(document.cookie.substring(start,end));
if (wc.substring(0,wc.indexOf("="))==myname) {
return wc.substring(wc.indexOf("=")+1);
}
}
return "";
}

Regards, [sig]<p>Russ<br><a href=mailto:bobbitts@hotmail.com>bobbitts@hotmail.com</a><br><a href= is in</a><br>[/sig]
 
I'll try it out.

Thanks for your help! :)

[sig][/sig]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top