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
Solved

Cookies storing values even after closing the browser.

Posted on 2009-07-14
17
729 Views
Last Modified: 2013-12-13
Hi all,
I am creating one cookie in a javascript and one cookie in a php page. I am setting them both with a expire date to '0' i.e of one cookie that's set:  setcookie("isQR","57",0,'/');

But still i see that when I close the browser and open another page, I can see that the cookies are still there, theoreticallly it should have been deleted after the browser is closed.
But why I am not seeing it happening. Please let me know how to proceed. I want the cookies to get deleted as soon as the browser is closed.

Thanks.
0
Comment
Question by:sandhya01
  • 3
  • 3
  • 2
  • +4
17 Comments
 
LVL 41

Expert Comment

by:HonorGod
ID: 24849758
If you don't specify an expiration time, then a cookie should be a session cookie, and expire when the browser closes.

Please show us the code for your setcookie() routine.
0
 
LVL 4

Expert Comment

by:SadafRasheed
ID: 24872935
just to add to what HonorGod said,, if you are using browsers with tabs then be careful while testing. closing tab is different from closing browser window.. session remains active even if you close the tab..
0
 

Author Comment

by:sandhya01
ID: 24885780
Hi Experts,
Please find the javascirpt code and php code for setting cookies in php and javascript page respectively.
After doing some testing i had the following observation:
1. If I open a single IE window and access the page in which cookies are set after I provide my login id and password, and then close it and if I try to open it again, I see that there are no cookies available, means closing the window destroys the cookies ---As per Expectation
2. If I open 2 different IE windows at the same time and then try to access the page in one of the IE windows and then close that particular IE window and If I try to open that page again in IE window I see that the cookies are still there - Not Per Expectation

So, somehow the second IE window is either delaying the deletion of cookies are not allowing them to net get deleted at all!, I am not able to understand this behaviour as the cookie is supposed to be set for a particular domain and the second IE window is not accessing any page in that domain( do I have to explicitly mention the domain also while setting cookie or by default the cookie takes the domain value in which it is created?).

Waiting for your expert suggestion

Thanks


//call to the function create cookies
createCookie('isQR','57',0,'/');
 
//actual function code
function createCookie(name, value, expires, path) {
	document.cookie = name + "=" + escape(value) + "; ";
	
	if(expires){
		expires = setExpiration(expires);
		document.cookie += "expires=" + expires + "; ";
	}
	if(path){
		document.cookie += "path=" + path + "; ";
	}
	
}
 
 
//php code to set cookie
setcookie("isQR","57",0,'/');

Open in new window

0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

 
LVL 35

Expert Comment

by:TimYates
ID: 24904109
How are you opening the two IE windows?

Using File->New Window will just give you two windows with the same session

At least it used to with IE6/7
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 24904192
What code do you have for setCookie() and setExpiration()?
0
 

Author Comment

by:sandhya01
ID: 24904764
Hi
Please find the setExpiration() code:
fuction setExpiration(CookieLife)
{
var today = new Date();
var expr = new Date(today.getTime() + cookieLife *24*60*60*1000)
return expr.toGMTString();
}

yes, I am opening two windows one by one seperately not as tab, I am using IE 6.0 and I am clicking desktop IE icon for opening IE.

Thanks


0
 
LVL 41

Expert Comment

by:HonorGod
ID: 24904897
ok, setExpiration() looks reasonable.

What about setCookie() ?
0
 
LVL 28

Expert Comment

by:Pravin Asar
ID: 24905079
Cookies are stored at client browser, so they are not deleted , but you set expiration time.

For a comprehensive cookie tutorial, look at

http://javascriptjournal.com/ws/?p=54

Please feel free to view source
0
 
LVL 50

Accepted Solution

by:
Steve Bink earned 125 total points
ID: 24909989
As per my own experiences with this very same issue awhile back, having any of the browser windows open will retain the session cookies.  This behavior is present in both IE and Firefox.  At least, it was when I did my tests with it about 3 years ago.  It looks like the browser software works on the principle that all windows must be closed for the browsing session to be over.

The only way I found to work around this is to expire the cookie manually, but that is not always possible.  You'll need to plan for this behavior in your application.
0
 
LVL 28

Expert Comment

by:Pravin Asar
ID: 24910102
Try not to cache the page.

header("Cache-Control: no-cache, must-revalidate");
header("Expires: Sat, 26 Jul 1997 05:00:00 EST");
0
 

Author Comment

by:sandhya01
ID: 24914461
Hi All,
Thanks for the comments...let me try out your comments...will let you know
0
 
LVL 54

Assisted Solution

by:b0lsc0tt
b0lsc0tt earned 125 total points
ID: 24943026
>> 2. If I open 2 different IE windows at the same time ... Not Per Expectation  <<
The comment at http:#a24909989 is correct.  Your expectation for the second case is wrong.  Cookies will remain as long as ANY window is open in that browser program.  Whether tab or its own window doesn't matter.  If you think about this it is actually a good design because otherwise the steps you expected would make it so the cookie couldn't be used by another browser window.  Definitely not behavior people would want.
If you want you could provide a logout or clear cookie link or button that will destroy the cookie (or at least clear the values).  That is a common step to have if you provide a logout option.
Let me know if you have a question about this.
bol
0
 
LVL 50

Expert Comment

by:Steve Bink
ID: 25982585
I recommend #24909989, but I may be biased.  :)
0
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 25992026
>> I recommend #24909989 <<

I agree.  I believe my post at http:#a24943026 adds a bit but the answer is in the comment at http:#a24909989 .

bol
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

This article discusses four methods for overlaying images in a container on a web page
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

828 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