Solved

Need to change cookie to expire on session close

Posted on 2004-09-20
2
312 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

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

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
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…
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…

687 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