Solved

Need to change cookie to expire on session close

Posted on 2004-09-20
2
307 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
ID: 12103540
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
ID: 12105639
Works Great..thanks
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

809 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