Solved

Need to change cookie to expire on session close

Posted on 2004-09-20
2
305 Views
Last Modified: 2010-05-18
Here is a cookie I am using that works great, but sets the expiration of the cookie to 1 year.  I need it to expire after each session.  In other words, when the user closes the browser I need the cookie to expire.  The script works great otherwise so I do not want to change anything EXCEPT the expiration to expire after each session.  If possible, please provide complete solution to script as I am not a Javascript programmer.  I found this script on a different EE posting and just changed the cookie name and value.

------------Set Cookie---------------------

<SCRIPT LANGUAGE="JavaScript">
<!--
dtNextYear = new Date();
dtNextYear.setTime(dtNextYear.getTime() + (365 * 24 * 60 * 60 * 1000)); // 365 days from now
setCookie('backupguard_login','BUGLoginYES',dtNextYear);
function setCookie(name, value, expire) {
document.cookie = name + "=" + escape(value) + ((expire == null) ? "" : ("; expires=" +expire.toGMTString()))
}
 //-->
</SCRIPT


-----------Check Cookie---------------------

<SCRIPT LANGUAGE="JavaScript">
      <!--
      strMyCookie = getCookie('backupguard_login');

      function getCookie(name) {
      var search = name + "="
      if (document.cookie.length > 0) {
      offset = document.cookie.indexOf(search)
      if (offset != -1) { // if cookie exists
      offset += search.length
      end = document.cookie.indexOf(";", offset)
      if (end == -1)
      end = document.cookie.length
      return unescape(document.cookie.substring(offset, end))
     }
     }
     }
      if (getCookie('backupguard_login') != 'BUGLoginYES') {
      document.location = 'http://www.backupguard.com/site/client_login.cfm';//if they haven't logged in
      }
      //-->
      </SCRIPT>
     
<script language="javascript" type="text/javascript">
0
Comment
Question by:murryc
2 Comments
 
LVL 49

Accepted Solution

by:
Roonaan earned 250 total points
Comment Utility
As far as I know you just (using the functions posted by you)

use setCookie('backupguard_login','BUGLoginYES') instead of setCookie('backupguard_login','BUGLoginYES', dtNextYear);

This will cause the cookie to have no explicit expire data, which will be interpreted by browsers as 'Expire on windowsclosed';

At least, this is how it usually works with cookies when they have no expire date set.
0
 

Author Comment

by:murryc
Comment Utility
Works Great..thanks
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now