...

View Full Version : cookie expiry date 2074?



d52477001
06-05-2003, 06:45 PM
hi, i have the following script to set a cookie:



<SCRIPT LANGUAGE="JavaScript"><!--
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" : "");
}
var today = new Date();
var zero_date = new Date(0,0,0);
today.setTime(today.getTime() - zero_date.getTime());

var todays_date = new Date(today.getYear()-71,today.getMonth(),today.getDate(),0,0,0);
var expires_date = new Date(todays_date.getTime() + (52 * 7 * 86400000)); // 52 weeks

var name = 'cookied';
var value = '52477001';

Set_Cookie(name,value,expires_date);
document.write(expires_date);
//--></SCRIPT>




Yet, there seems to be a problem with the expiry date. It supposed to set it to the current time and date + the specified one. Now the problem is that it sets the xpiry date to midnight 2074... is it not possible to do this or is there a problem in the script layout?

d52477001

d52477001
06-07-2003, 11:32 PM
really nobody has an idea?

Mr J
06-08-2003, 10:44 AM
Please try the following


<SCRIPT LANGUAGE="JavaScript"><!--
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" : "");
}

from="08/06/2003"
to="08/06/2074"

var fromArray = from.split('/');
fromdate = new Date( fromArray[2], fromArray[1]-1, fromArray[0]);

var toArray = to.split('/');
todate = new Date( toArray[2], toArray[1]-1, toArray[0]);

count=(todate-fromdate)/86400000

var expires_date = new Date();
expires_date.setTime(expires_date.getTime() + (count*24*60*60*1000));


var name = 'cookied';
var value = '52477001';

Set_Cookie(name,value,expires_date);
document.write(expires_date);
//-->
</SCRIPT>

d52477001
06-08-2003, 12:33 PM
it seems that you have missunderstood my question. i want the cookie to expire after, say, 15 minutes. is this possible?

d52477001

Mr J
06-08-2003, 01:57 PM
Please try the following


<SCRIPT LANGUAGE="JavaScript"><!--
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" : "");
}

time=15 // IN MINUTES

var expires_date = new Date();
expires_date.setTime(expires_date.getTime() + (time*60*1000));

var name = 'cookied';
var value = '52477001';

Set_Cookie(name,value,expires_date);
document.write(expires_date);
//-->
</SCRIPT>

d52477001
06-11-2003, 09:45 PM
yep, that did it!

thanks very much...

d52477001



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum