Solved

How do I clear all session variables once browser is closed + php

Posted on 2010-09-09
7
517 Views
Last Modified: 2012-05-10
Hi All,

How do I clear all session variables once browser is closed in php

Thanks in Advance.
0
Comment
Question by:Web_Sight
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 25

Expert Comment

by:Lee Savidge
ID: 33635384
0
 
LVL 4

Expert Comment

by:antonybrahin
ID: 33635395
I use the below to do that.

<% Response.CacheControl = "no-cache"; %>
<% Response.AddHeader("Pragma", "no-cache"); %>
<% Response.Expires = -1; %>
0
 
LVL 10

Expert Comment

by:bugada
ID: 33635493
weird question! You server can't know when you close your browser unless you send a logout messagge to the server before closing it, or maybe you implement a timeout function based on the inactivity of that session. Then you can do what suggested by isadvige.
0
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.

 
LVL 4

Expert Comment

by:antonybrahin
ID: 33635531
the session will be cleared when you reopen the site as we use no-cache
0
 
LVL 25

Accepted Solution

by:
Lee Savidge earned 500 total points
ID: 33635601
If you're trying to detect a browser close by the user, PHP won't pick that up as it is a server side language. You'd need to implement a javascript function that checks for the onunload and onbeforeunload events to post back to the server for a PHP script to pick up and then use the code in the links I provided.

Lee
0
 
LVL 10

Expert Comment

by:bugada
ID: 33636031
antonybrahin no-cache prevents caching (and it's not assured thought) and it has nothing to do with sessions.

If you close your browser, you invalidate your session, but the server is not informed about that.
If you reopen the browser a new session i established, so for the server there are two session actives.

Your method is probably influenced by a IE bug
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 33637757
You might want to learn about session garbage collection.  It's not exactly intuitive.  Some good reading here:
http://us2.php.net/manual/en/session.idpassing.php
http://www.php.net/manual/en/session.configuration.php#ini.session.gc-maxlifetime
http://www.php.net/manual/en/ref.session.php

In your case the session is almost certainly associated with a session cookie.  The cookie has a value that enables PHP to find a pointer to the session data on your server.  The cookie has an expiration date of zero, meaning that it expires when the last instance of the browser is closed.  Notice that I said, "browser" and not "window" or "tab" -- the difference is important.  Your browser has a single cookie jar, so all instances of all windows or tabs will return all the cookies.  This results in a common confusion among developers who test their login and logout scripts with multiple copies of Firefox running at the same time.

Once the browser is (completely) closed the session cookie is gone forever.  The discarded session data lives on your server until garbage collection, but it is in the dead zone, since there is no cookie anywhere that can enable PHP to find it any more.

If you're interested in the correct way to write a logout routine, you might find the example in this article helpful.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_2391-PHP-login-logout-and-easy-access-control.html?sfQueryTermInfo=1+30+login+logout

Regards, ~Ray
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

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 how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

746 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