Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 536
  • Last Modified:

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

Hi All,

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

Thanks in Advance.
0
Web_Sight
Asked:
Web_Sight
  • 2
  • 2
  • 2
  • +1
1 Solution
 
antonybrahinCommented:
I use the below to do that.

<% Response.CacheControl = "no-cache"; %>
<% Response.AddHeader("Pragma", "no-cache"); %>
<% Response.Expires = -1; %>
0
 
bugadaCommented:
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
antonybrahinCommented:
the session will be cleared when you reopen the site as we use no-cache
0
 
Lee SavidgeCommented:
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
 
bugadaCommented:
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
 
Ray PaseurCommented:
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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 2
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now