Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
|
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 21st, 2008, 06:41 AM
Registered User
 
Join Date: Nov 2008
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default delete session when tab window closes

some one asked this question. "My page create session using userid when user login ,how can i clear session variables when user closes browser/windows using which he has logged." In IE there are tabs. Yes if the user closes the browser the session data is destroyed. and yes if he logs out. BUT, if he closes just the tab the site is on, the information is NOT destroyed. is there anyway to have the session time out or sense that the tab window it was in is now closed and destroy the session?

 
Old December 17th, 2008, 09:32 PM
Registered User
 
Join Date: Dec 2008
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Maybe this will help:

Source from:
HTML Code:
    <html>
        <head>
            <script type="text/javascript">
                flag=true;
                window.onbeforeunload = function(){if(flag)return "Warning you are still logged in, are you sure?";};
            </script>
        </head>
        <body>
            <a href="http://google.com" onclick="flag=false;">link test</a>
            <form onsubmit="flag=false;">
                <input type="submit"></input>
            </form>
        </body>
    </html>
Or Advanced:

if you use the Javascript HTTP Request with the previous source:
ExamplesThen you can call the PHP page with:
PHP Code:
  session_destroy(); 
Example:
 
Old February 21st, 2009, 05:02 PM
Friend of Wrox
 
Join Date: Sep 2005
Posts: 166
Thanks: 2
Thanked 33 Times in 33 Posts
Default

GreenCore's example are correct and can be used to either keep the user on the page or to force the session to end.
It all comes down to being able to send a request for a logout page to the server so that it can kill the session. However you cannot rely on any solution successfully ending the session, as they generally use JavaScript, which can be turned off, or the user's connection may die etc....

In terms of putting a timeout on the session, PHP by default will do this anyway, as it uses cookies (be it client or server) to control user's sessions, and these have an expiry date of a specified time after the user's last activity on the site - I believe the default is 24 minutes. It is possible to control the timeout length.
This page and the links on it may help:
http://www.webmasterworld.com/forum88/12812.htm

Hope this helps
Phil





Similar Threads
Thread Thread Starter Forum Replies Last Post
Session value in child window balesh.mind ASP.NET 1.0 and 1.1 Professional 1 March 12th, 2007 05:16 AM
Detect when window closes, not unload Snib Javascript How-To 11 October 24th, 2006 01:14 AM
how to destroy session when window is closed chayanvinayak PHP How-To 1 June 1st, 2006 06:18 AM
a problem of adding a confirm window to delete but raybristol ASP.NET 1.0 and 1.1 Basics 1 December 12th, 2005 05:01 PM
a problem of adding a confirm window to delete but raybristol ADO.NET 1 December 12th, 2005 04:50 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.