How do I make this code below into a session cookie that deletes once the page is closed? - <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>
Thanks
3 2267 Dormilich 8,658
Recognized Expert Moderator Expert
if you want to delete the cookie when the page is closed, you have to do that by JavaScript itself (e.g. the unload or beforeunload event). if it suffices to delete the cookie when the browser is closed, set the cookie lifetime to 0.
To set the life of the cookie to 0 would i do this? - <script>
-
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 = 0;
-
expiresDate = new Date( today.getTime() + (expires) );
-
}
-
-
// if "rememberMe" is false, "expiresDate" has no value
-
-
return expiresDate;
-
}
-
</script>
Will that make it a session cookie?
Thanks
Dormilich 8,658
Recognized Expert Moderator Expert
The Expires directive tells the browser when to delete the cookie. It is specified in the form of “Wdy, DD-Mon-YYYY HH:MM:SS GMT”, indicating the exact date/time this cookie will expire.
… if you just omit the Expires directive, the lifetime is set as 0
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Henry Hartley |
last post by:
I've got a PHP system working on a development server (Windows
2000/IIS5/PHP 4.3.3) but it doesn't seem to be working quite right on
the testing server (same except PHP 4.2.3). I upgraded the PHP...
|
by: Matt |
last post by:
I want to know what's the differences between session cookie and regular
cookie.
In ASP, when we create cookie, we do the following to identify an user:
Response.Cookies("name") = value
Is...
|
by: M Wells |
last post by:
Hi All,
Just wondering how you go about changing the value of a session cookie
via javascript?
I have a PHP page that sets a session cookie when it first loads. I'd
like to be able to change...
|
by: Yossi |
last post by:
I want to set the asp session id path property.
how can I do that?
I mean, asp session id is stored in a cookie and like any other cookie
it should have properties (or attributes), how can I...
|
by: Karsten Grombach |
last post by:
Hi,
I'm trying the following:
- Imitate a Logon using a Post with HttpWebRequest on remote Webserver (asp
3.0 page using https)
- On success redirect to the page (encapsuled in an iframe)...
| |
by: Earl Teigrob |
last post by:
I am trying to find storage mechanism that will store a variable until the browser window is closed. A query string variable would be my first choice for this but that is not easily done in my...
|
by: Daniel Michaeloff |
last post by:
Hi all,
I have an application that when finished redirects to a non-ASP.NET app
which is choking on a huge ASP.NET session cookie. The cookie
"ASP.NET_SessionId" gets transmitted by the browser...
|
by: Doug |
last post by:
An ASP.NET session cookie set on "www.mydomain.com" can not be accessed on
"search.mydomain.com"; hence, a new session and cookie are being created on
every sub-domain.
This is occuring because...
|
by: nico |
last post by:
I store the authenticated user in a session cookie. This cookie expires after
an hour. Now for some reason sometimes the client browser decides not to send
the session cookie information with a new...
|
by: Ibrahim. |
last post by:
Hi,
Kindly clarify the followings;
1. Is the session cookie & FormsAuthenticationTicket cookie Interdependent?
2. When a new SessionID is generated, I have noticed the Session Cookie and...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |