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?

 

 

Link to comment
Share on other sites

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;
}
}  
}  
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.