Solved

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

Posted on 2010-09-09
7
527 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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 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 109

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

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 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…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
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…
The viewer will learn how to dynamically set the form action using jQuery.

820 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