How do I make this code below into a session cookie that deletes once the page is closed?
Thanks
Code:
<script>
/*
Very simple validation for the login.
*/
function validateLogin()
{
var username = document.getElementById("username").value;
var pwd = document.getElementById("password").value;
// encrypt the username and password
var userEntryEncryption = hex_md5 (username + pwd);
/*
Encrypt "username" + "books" and compare to what the user entered in the
username and password fields. The comparison takes the 'hash' of the
'userEntryEncryption' variable and the 'md5Encryption' variable and test
whether they match.
NOTE: On a production site, the 'userEntryEncryption' would be sent to some server
side code on a webserver, which would perform the validation and return true or
false, depending on whether it succeeded.
*/
var md5Encryption = hex_md5 ("username" + "books");
// both hashes must match
if (userEntryEncryption === md5Encryption)
{
/*
Determine the expiry for the cookie depending on whether the user
wants the login information remembered.
*/
var remember = document.getElementById("rememberMe").checked;
var expiry = getCookieExpiry(remember);
// the value of our cookie is the encrypted username + password
setCookie(md5Encryption, expiry);
// redirect to the "members" page
document.location.href = "checkout2.html";
return true;
}
document.getElementById("errorMessage").innerHTML =
"The login information is incorrect. Please try again.";
return false;
}
/*
Sets the cookie for a single logon.
*/
function setCookie(encryptedPwd, expiry)
{
var key = "userLogin";
var value = encryptedPwd;
var path = "/";
document.cookie = key + "=" +escape( value ) +
( ( expiry ) ? ";expires=" + expiry.toGMTString() : "" ) +
( ( path ) ? ";path=" + path : "" );
}
/*
If the user wants the login information remembered
(i.e. "rememberMe" is true), set a long expiry date,
otherwise, leave the expiry date empty so that the cookie
will be deleted when the browser is closed.
*/
function getCookieExpiry(rememberMe)
{
var expiresDate;
if (rememberMe === true)
{
// set time, it's in milliseconds
var today = new Date();
today.setTime( today.getTime() );
/*
This will set the expiry date for x number of days.
To make it for hours, delete * 24, for minutes,
delete * 60 * 24.
*/
var expires = 365;
expires = expires * 1000 * 60 * 60 * 24;
expiresDate = new Date( today.getTime() + (expires) );
}
// if "rememberMe" is false, "expiresDate" has no value
return expiresDate;
}
/*
Returns the cookie associated with the "key" variable.
*/
function getCookie( key )
{
// first we'll split this cookie up into key/value pairs
// note: document.cookie only returns key=value, not the other components
var allCookies = document.cookie.split( ';' );
var tempCookie = '';
var cookieName = '';
var cookieValue = '';
var cookieFound = false; // set boolean true/false, default is false
for ( i = 0; i < allCookies.length; i++ )
{
// now we'll split apart each key=value pair
tempCookie = allCookies[i].split( '=' );
// and trim left/right whitespace while we're at it
cookieName = tempCookie[0].replace(/^\s+|\s+$/g, '');
// if the extracted cookieName matches the "key" parameter
if ( cookieName == key )
{
cookieFound = true; // cookie is found
// we need to handle case where cookie has no value but exists (no = sign, that is):
if ( tempCookie.length > 1 )
{
cookieValue = unescape( tempCookie[1].replace(/^\s+|\s+$/g, '') );
}
break;
}
tempCookie = null;
cookieName = '';
}
if ( !cookieFound ) // the user has not logged in yet so go to the login page
{
location.href = "checkout.html";
}
// otherwise, set the CSS style to visible
else
{
document.getElementById('content').style.visibility = "visible";
}
}
</script>
Comment