PDA

View Full Version : Creating a cookie (with expiry date)



Macca
09-13-2010, 03:25 PM
I have had trouble creating a cookie with an expiry date set in the fututre.

Grateful for any help.


<script language="JavaScript">
<!--
function setcookie(name, value, store) {
if (store >= 1) {
var expire = new Date ();
if (store == 1) {
expire.setTime (expire.getTime() + (365*24*60*60*1000));
}
if (store == 2) {
expire.setTime (expire.getTime() + (1*24*60*60*1000));
}
} else {
var expire = null;
}
document.cookie = name + "=" + escape(value) + "expires=Mon, 14 Sep 2020 18:49:22 GMT";
}
function cookie_icons_offset() {
var now = new Date ();
var browser_time = now.getTime();
setcookie("lastvisit", browser_time, 1);
var server_now = "1284390825";
if (server_now != "") {
var offset = browser_time - (1000*server_now);
setcookie("offset", offset, 1);
}
}
// -->
</script>

vwphillips
09-13-2010, 05:26 PM
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
<title></title>
<script language="JavaScript">
<!--

function setcookie(name, value, store) {
var days=store==1?365:store==2?1:-1;
document.cookie = name + "=" + value + ";expires="+(new Date(new Date().getTime()+days*86400000).toGMTString())+";path=/";
}

function cookie_icons_offset() {
var now = new Date ();
var browser_time = now.getTime();
setcookie("lastvisit1", browser_time, 1);
var server_now = "1284390825";
if (server_now != "") {
var offset = browser_time - (1000*server_now);
setcookie("offset1", offset, 1);
}
}

function zxcReadCookie(nme){
nme+='=';
var split = document.cookie.split(';');
for(var z0=0;z0<split.length;z0++){
var s=split[z0];
while (s.charAt(0)==' ') s=s.substring(1,s.length);
if (s.indexOf(nme)==0) return s.substring(nme.length,s.length);
}
return null;
}
// -->
</script></head>

<body>
<script type="text/javascript">
/*<![CDATA[*/
cookie_icons_offset();
alert(zxcReadCookie("offset1")+'\n'+zxcReadCookie("lastvisit1"));
/*]]>*/
</script>
</body>

</html>

jscheuer1
09-13-2010, 08:39 PM
See also:

http://www.dynamicdrive.com/forums/blog.php?b=32

Macca
09-15-2010, 11:38 AM
Thanks for your help. I have now set the expiry date but I seem to have an issue with the cookie storing username and passwords.

For some strange reason the username and password are expiring on the 6th September. I am using a discussion forum app called discusware 4.0. Not sure were the expiry date is set. What I want to do is update the function initpage to set the expiry date myself. Grateful for any help.


<script language="javascript">
<!-- ;
function initpage () {
if (document.userpass) {
if (document.userpass.username) {
if (document.userpass.username.value == "") {
document.userpass.username.value = readcookie("user$COOKIE_ID")

}
}
if (document.userpass.passwd) {
if (document.userpass.passwd.value == "") {
document.userpass.passwd.value = readcookie("rpwd$COOKIE_ID")

}
}
}
<#if $GLOBAL_OPTIONS{cookie_for_new} == 1#>
cookieinit();
}
<#insert part ($head->{topic_number}) "cookie_icons"#>
if (document.images) {
<#foreach $subtopic (@subtopics)#>
<#if $subtopic->{lastmod} ne ""#>
if (($subtopic->{lastmod} - server_gmt_offset) > compare_time) {
document.img$subtopic->{number}.src = img_new.src;
}
<#endif#>
<#endloop#>
}
<#endif#>
}
<#insert part ($head->{topic_number}) "readcookie"#>
// end hide -->
</script>