Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Aug 2006
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Saving & deleting cookies

    I have your standard "remember me?" with not so standard radio buttons (yes/no). I was unable to modify this to use a checkbox, so any help is appreciated. But, the main reason for the post is I am having trouble with deleting the cookie when I comment out delete username. I would like to save the username regardless and only save/delete the password.

    Code:
    Code:
    <script language="JavaScript">
    <!--
    //
    //  Cookie Functions -- "Night of the Living Cookie" Version (25-Jul-96)
    //
    //  Written by:  Bill Dortch, hIdaho Design <bdortch@hidaho.com>
    //  The following functions are released to the public domain.
    //
    function getCookieVal (offset) {
      var endstr = document.cookie.indexOf (";", offset);
      if (endstr == -1)
        endstr = document.cookie.length;
      return unescape(document.cookie.substring(offset, endstr));
    }
    function GetCookie (name) {
      var arg = name + "=";
      var alen = arg.length;
      var clen = document.cookie.length;
      var i = 0;
      while (i < clen) {
        var j = i + alen;
        if (document.cookie.substring(i, j) == arg)
          return getCookieVal (j);
        i = document.cookie.indexOf(" ", i) + 1;
        if (i == 0) break; 
      }
      return null;
    }
    function SetCookie (name,value,expires,path,domain,secure) {
      document.cookie = name + "=" + escape (value) +
        ((expires) ? "; expires=" + expires.toGMTString() : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
    }
    
    function DeleteCookie (name,path,domain) {
      if (GetCookie(name)) {
        document.cookie = name + "=" +
          ((path) ? "; path=" + path : "") +
          ((domain) ? "; domain=" + domain : "") +
          "; expires=Thu, 01-Jan-70 00:00:01 GMT";
      }
    }
    // -->
    </script>
    
    <script language="JavaScript">
    var usr;
    var pw;
    var sv;
    function getme()
    {
    usr = document.loginform.username;
    pw = document.loginform.password;
    sv = document.loginform.save;
    
    	if (GetCookie('username') != null)
    	{
    		usr.value = GetCookie('username')
    	pw.value = GetCookie('password')
    		if (GetCookie('save') == 'true')
    		{
    			sv[0].checked = true;
    		}
    	}
    }
    function saveme()
    {
    	if (usr.value.length != 0 && pw.value.length != 0)
    	{
    		if (sv[0].checked)
    		{
    			expdate = new Date();
    			expdate.setTime(expdate.getTime()+(365 * 24 * 60 * 60 * 1000));
    			SetCookie('username', usr.value, expdate);
    		             SetCookie('password', pw.value, expdate);
    			SetCookie('save', 'true', expdate);
    		}
    		if (sv[1].checked)
    		{
    			DeleteCookie('username');
    		             DeleteCookie('password');
    			DeleteCookie('save');
    		}
    	}
    		else
    	{
    		alert('You must enter a username and password.');
    		return false;
    	}
    }
    </script>
    My edit:

    Code:
    //			DeleteCookie('username');
    Deleting the cookie no longer works when I comment out this line. Any idea?

  • #2
    Senior Coder
    Join Date
    Apr 2005
    Posts
    1,051
    Thanks
    0
    Thanked 0 Times in 0 Posts
    try this

    right now your setting the cookie exp. date to jan first 2070

    so obviously the cookie is not being deleted.

    Delete Cookie Javascript Function

    Code:
    // this deletes the cookie when called
    function Delete_Cookie( name, path, domain ) {
    if ( Get_Cookie( name ) ) document.cookie = name + "=" +
    ( ( path ) ? ";path=" + path : "") +
    ( ( domain ) ? ";domain=" + domain : "" ) +
    ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
    }

    Here all you need to do is put in: Delete_Cookie('cookie name', '/', '') and the cookie will be deleted. Remember to match the cookie name, path, and domain to what you have it in SetCookie exactly, or you may get some very hard to diagnose errors.

    WARNING: Do not assume the Cookie has actually been deleted during that session, both Opera and IE have in some of their browser versions maintained the cookie until you restart your browser even if you have deleted it in the script. If script functionality depends on the delete action having happened, make sure you do another GetCookie test on the deleted cookie to make sure it's actually deleted.



    however from what i'm seeing it looks like you're putting too much effort into this thing.


    You want cookies to store and manage user logins? as well as remember the status of a checkbox?
    public string ConjunctionJunction(string words, string phrases, string clauses)
    {
    return (String)(words + phrases + clauses);
    }
    <--- Was I Helpfull? Let me know ---<

  • #3
    Senior Coder
    Join Date
    Apr 2005
    Posts
    1,051
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Here is what i use. i have placed it in a small sample file so you can see how it's used.

    Code:
    <html>
    <head><title>example cookie script by rlemon</title>
    <script language="javascript" type="text/javascript">
    
    // TESTING
    
    window.onload=function()
    {
    setCookie('NAME','VAL'); // days are not required, however are good to add (see below)
    setCookie('NEWNAME','val',4);
    document.body.innerHTML += getCookie('NEWNAME');
    document.body.innerHTML += "<br />";
    killCookie('NAME');
    document.body.innerHTML += getCookie('NAME');
    }
    
    // END TESTING
    
    // COOKIE CODE
    
    function setCookie(name,value,days)
    {
    	var expires = "";
    	if (days)
    	{
    		var date = new Date();
    		date.setTime(date.getTime()+(days*24*60*60*1000));
    		expires = "; expires="+date.toGMTString();
    	}
    	document.cookie = name+"="+value+expires+"; path=/";
    }
    
    function getCookie(name)
    {
    	var nEQ = name + "=";
    	var ca = document.cookie.split(';');
    	for(var i=0;i < ca.length;i++)
    	{
    		var c = ca[i];
    		while (c.charAt(0)==' ') c = c.substring(1,c.length);
    		if (c.indexOf(nEQ) == 0) return c.substring(nEQ.length,c.length);
    	}
    	return null;
    }
    
    function killCookie(name)
    {
    	setCookie(name,"",-1);
    }
    
    // END COOKIE CODE
    </script>
    </head>
    <body>
    </body>
    </html>
    setCookie('CookieName','CookieValue',0); // Name, Value, Number of days you want it to be active.

    You can use this code to update cookies as well, just ensure the same name.

    getCookie('name') returns the cookie value.

    and killCookie('name') erases it.
    public string ConjunctionJunction(string words, string phrases, string clauses)
    {
    return (String)(words + phrases + clauses);
    }
    <--- Was I Helpfull? Let me know ---<


  •  

    Posting Permissions

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