PDA

View Full Version : onbeforeunload => logout if closed? Working in IE only!



student101
08-25-2009, 10:14 PM
Had a question regarding logging out users who don't click Logout, who instead just close the browser.

I have two scripts pretty much the same but they only work in IE due to that fact that onbeforeunload was implemented for IE.

The latest version;

window.onbeforeunload = confirmExit;
var aClick=false;
function confirmExit(e){
if(document.all)e = event;
if(!e)e=window.event;
if(e){
if(aClick==false && (e.target==document || e.clientX<0 || e.clientY<0)) {

//alert("Logging out...");
// Add server call to logout the user

/* Original code commented out because of page refresh, page reloads or form submits.

alert('about to open the window');
ajaxCall("http://example.com/logout.php");
*/
//adding this allows for page refresh, page reloads or form submits in IE only
function open_win() {
ajaxCall("http://example.com/logout.php");
}
open_win()

}
}
}
function ajaxCall(dname){
var xmlDoc;
if (window.XMLHttpRequest){
xmlDoc = new window.XMLHttpRequest();
xmlDoc.open("GET", dname, false);
xmlDoc.send("");
//alert(xmlDoc.responseText); //Hide blank alert boxes.
//return xmlDoc.responseXML; //Comment out - not needed.
// IE 5 and IE 6
}else if (ActiveXObject("Microsoft.XMLDOM")){
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = false;
xmlDoc.load(dname);
//alert(xmlDoc.responseText); //Hide blank alert boxes.
//return xmlDoc; //Comment out - not needed.
}
//alert("Error loading document"); //Hide blank alert boxes.
return null;
}

Is there any method or way to get this to work in Firefox?
If Yes, how?

student101
08-26-2009, 07:11 PM
I've searched and not once has this question been answered along with any kind of code that works.

thenajsays
08-26-2009, 08:19 PM
why not just use php session cookies instead?

student101
08-26-2009, 08:50 PM
Can it logout the user if they haven't clicked the logout, who instead closed the window?

On my search for this method, haven't seen one example?

Searching for php session cookies brings up different methods of doing this.
Will explore...
Thank you!

thenajsays
08-26-2009, 09:06 PM
it does it automatically i believe... im still learning php, but theres A LOT you can do... let me look and see how i did it and ill get back to you

thenajsays
08-26-2009, 09:32 PM
ok, first things first, every page that you want the session to be vaild for, you have to have:

<?php
session_start();
?>

BEFORE ANY HTML
then, you need to have a little snippet of code like this somewhere to check if the session has been initialized, like this:

<?php
if (!isset($_SESSION['cookieName']))
{
/*Do something*/
} else
{
/*Do something else*/
}
?>

and finally, in your log in validation, if the username and password match the data base, you have to initialize the cookie:

$_SESSION['cookieName'] = 'sessionID';

the session ID can be a variable (such as the username)
oh... one more thing... on the log out page you need to destroy the session, like so:

<?php
session_start();
session_destroy();
?>
again, that has to go BEFORE any HTML

student101
08-26-2009, 09:58 PM
Good one, you got the jump on me.
Cool let me play, read and post.

Luckily you brought this up.
Thanks again thenajsays!

thenajsays
08-26-2009, 10:02 PM
dont forget to click the thanks button for me! and let me know how it turns out!

student101
08-27-2009, 05:21 AM
Did that, just woke up.
Off to code!

Cheers