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 2272 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 on the
testing server but that didn't seem to make any difference. One
difference I noticed is that on the dev server, the session cookie is...
|
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 this regular cookie? How about session cookie? and when to use which?
|
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 the value of that session cookie in response
to a button click in a form, without resubmitting the page.
For some reason, the following doesn't...
|
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 control it?
Yossi.
|
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) supplied by the
remote Webserver
I can successfuly logon but when I redirect to the supplied url, the
webserver does not know me anymore an redirects...
| |
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 situation. Would a session cookie do the trick? From what I have read, it is persistent until the browser window closes. Is that correct? Even if the...
|
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 (IE6 in test case)
despite trying Microsoft's suggested method of expiring the cookie first.
I'd like to be able to kill the cookie and do a...
|
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 ASP.NET always sets the Session cookie domain to
the full domain (e.g. "www.mydomain.com") instead of the parent domain (e.g.
"mydomain.com")
The...
|
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 request. This means the server
receives a request without user information and redirect the user to the
login page.
Somebody any bright ideas?
...
|
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
the FormsAuthentication cookie are binded.
3. I need to know whether it's possible by Code or settings to treat the
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
| |
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes...
|
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 we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
| |