Solved

Need to change cookie to expire on session close

Posted on 2004-09-20
2
311 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
This article discusses how to create an extensible mechanism for linked drop downs.
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…

739 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