...

View Full Version : Saving & deleting cookies



iceblinkmedia
08-03-2006, 05:40 PM
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:

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


// DeleteCookie('username');

Deleting the cookie no longer works when I comment out this line. Any idea?

rlemon
08-03-2006, 07:05 PM
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


// 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?

rlemon
08-03-2006, 08:08 PM
Here is what i use. i have placed it in a small sample file so you can see how it's used.



<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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum