Jump to content

onbeforeunload => logout if closed, logged out or crashed?


student101

Recommended Posts

How to code in the event of a PC crash or power out or closing the browser(any)?

 

Users login one at a time;

A User logs in => The DB value (loggedin) is set to 'Y'

The User makes changes....

The User Logs out => The DB value (loggedin) is set to 'N' (not logged in)

Other users can now login.

 

What if the users PC crashes or there's a Power out or even closing the browser(any)??

The user didn't click Logout!, other users can't login

The DB value (loggedin) is still 'N'

CREATE TABLE `tbladmin` (
`id` INT(3) UNSIGNED NOT NULL AUTO_INCREMENT,
`username` VARCHAR(15) NOT NULL DEFAULT '',
`password` VARCHAR(15) NOT NULL DEFAULT '',
`loggedin` VARCHAR(2) NULL DEFAULT 'N',
`logdate` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`adminid`)
)

 

The only idea that came to mind was to have this above, other than, clueless.

Any ideas?

 

 

Here is a working solution for IE only!

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("Loggined out...");  //remove don't want an alert box!
// add your server call to logout the user

ajaxCall(open_win());
function open_win() {
ajaxCall("http://example.com/logout.php");
//or you could call a popup; window.open("http://example.com/logout.php"); //actually irritating! 
}

function ajaxCall(dname) {
var xmlDoc;
if (window.XMLHttpRequest) {
xmlDoc = new window.XMLHttpRequest();
xmlDoc.open("GET", dname, false);
xmlDoc.send("");
//alert(xmlDoc.responseText); //removed due to blank alert box!
//return xmlDoc.responseXML; //removed, no clue why!
}
// IE 5 and IE 6
else if (ActiveXObject("Microsoft.XMLDOM")) {
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = false;
xmlDoc.load(dname);
//alert(xmlDoc.responseText); //removed due to blank alert box!
//return xmlDoc; //removed, no clue why!
}
//alert("Error loading document"); //removed due to blank alert box!
return null;
}
}  
}  
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.