|
-
Mar 16th, 2006, 08:48 AM
#1
Thread Starter
Addicted Member
[RESOLVED] Session destroy when closing browser?
Hi,
How do I destroy a session when the browser window with my session data closes?
-
Mar 16th, 2006, 08:53 AM
#2
<?="Moderator"?>
Re: Session destroy when closing browser?
session_destroy() will, as you've probearly guesssed, destroys all the sesssion data. The problem only occurs when calling the function, but this can be resolved by calling an AJAX function or opening a new window to the location of your script by adding onUnload to the body tag or in javascript using
Code:
<script>
window.onunload = function(){alert('Window Closed.')}
</script>
-
Mar 16th, 2006, 08:54 AM
#3
Fanatic Member
Re: Session destroy when closing browser?
Code:
<?php
session_start();
session_destroy();
?>
-
Mar 16th, 2006, 08:57 AM
#4
Thread Starter
Addicted Member
Re: Session destroy when closing browser?
Ehm, how shall I use that? :P
-
Mar 16th, 2006, 09:00 AM
#5
<?="Moderator"?>
Re: Session destroy when closing browser?
create a script, like kenny_oh said, called destroy.php like
PHP Code:
<?php
session_start();
session_destroy();
?>
<script language="javascript" type="text/javascript">
window.close();
</script>
and in your main script, the window which the user will be closing add the following script to it.
PHP Code:
<script language="javascript" type="text/javascript">
window.onunload = function(){window.open('destroy.php');}
</script>
-
Mar 16th, 2006, 11:58 AM
#6
Thread Starter
Addicted Member
Re: Session destroy when closing browser?
Cool, guys! Thanks. But:
1) How can it be programmed so you can't see that window popping?
2) Is it possible to make it purely in PHP so the user won't have to download java?
-
Mar 16th, 2006, 12:10 PM
#7
<?="Moderator"?>
Re: Session destroy when closing browser?
Its not possible to do it purely in PHP as PHP is server side and what you want is using client side. To do it without a window popping up then you need to use AJAX which creates a HTML request.
Code:
<script language="javascript" type="text/javascript">
function createRequestObject()
{
var request_o; //declare the variable to hold the object.
var browser = navigator.appName; //find the browser name
if(browser == "Microsoft Internet Explorer"){
/* Create the object using MSIE's method */
request_o = new ActiveXObject("Microsoft.XMLHTTP");
}else{
/* Create the object using other browser's method */
request_o = new XMLHttpRequest();
}
return request_o; //return the object
}
var http = createRequestObject();
function destrorySession()
{
try
{
http.open('get', 'destroy.php');
http.send(null);
}
catch(Exception)
{
http = createRequestObject();
destorySession();
}
}
window.onunload = destorySession();
</script>
-
Mar 16th, 2006, 12:17 PM
#8
Thread Starter
Addicted Member
Re: Session destroy when closing browser?
Great!
Now, I just found out that the pages logs you off when refreshing. Can that be disabled?
-
Mar 17th, 2006, 05:31 AM
#9
Re: Session destroy when closing browser?
Hmm, this smells clunky to me. Are you aware that sessions time out automatically after a relatively short period of time anyway? There's not a lot of point in guesstimating when the user closes the window and destroying session data manually. There's not really a reliable way to tell the difference between closing the window and refreshing.
-
Mar 17th, 2006, 08:11 AM
#10
<?="Moderator"?>
Re: Session destroy when closing browser?
Is this what banks and other secure places use to make sure that all session information is clear in case the user is accessing the site from a public place, or where someone else is going to use the computer after them. Then they probearly wouldn't want to chance it that the session will time out before the next user arrives
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|