Results 1 to 4 of 4

Thread: Creating a cookie (with expiry date)

  1. #1
    Join Date
    Feb 2009
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Creating a cookie (with expiry date)

    I have had trouble creating a cookie with an expiry date set in the fututre.

    Grateful for any help.

    Code:
    <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>

  2. #2
    Join Date
    Dec 2008
    Location
    Portsmouth, UK
    Posts
    1,891
    Thanks
    2
    Thanked 441 Times in 435 Posts

    Default

    Code:
    <!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>
    Vic
    God Loves You and will never love you less.
    http://www.vicsjavascripts.org/Home.htm
    If my post has been useful please donate to http://www.operationsmile.org.uk/

  3. #3
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  4. #4
    Join Date
    Feb 2009
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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.

    Code:
    <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>

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •