Jump to content

Onbeforeunload => Logout If Closed, Logged Out Or Crashed?


student101
 Share

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 loginThe 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

Why would you only allow one user to log in at a time?In any case, you shouldn't rely on Javascript to tell when the user logs out. You should just store the information of when a user was last online and consider them offline if more than five minutes have passed since then.

Link to comment
Share on other sites

Why would you only allow one user to log in at a time?In any case, you shouldn't rely on Javascript to tell when the user logs out. You should just store the information of when a user was last online and consider them offline if more than five minutes have passed since then.
To answer that question, I don't know? - I was asked about login that only allows one user at a time.What about;
<?php $today =  date('Y-m-d H:i:s');$login_length = 15;$timestamp = strtotime("$today");$etime = strtotime("+$login_length minutes", $timestamp);$next_time = date('Y-m-d H:i:s', $etime); if( $next_time > $row_rsuser['logdate']){ echo "Time since last login is greater, Go Login";// do stuff here if needed...}else{echo "Time since last login is Less than 15 minutes ago, Please check back in Fifteen";// do stuff here if needed...}?>

It still doesn't really do much! except give a waiting period, the other user could be on for an hour.

Edited by student101
Link to comment
Share on other sites

Every time a logged in user refreshes the page, you update the timestamp. When somebody else views the page the timestamp isn't updated. That's how you know generally when somebody has logged out.

Link to comment
Share on other sites

Every time a logged in user refreshes the page, you update the timestamp. When somebody else views the page the timestamp isn't updated. That's how you know generally when somebody has logged out.
Not really sure what you mean?You are saying that the LOGGED IN user refreshes the TIMESTAMP with each page they load?That's possible;I was thinking of a timer inside the admin panel that counts down from 15min to 0 and they can choose to stay LOGGED IN and refresh the timestamp.The new user would need to check back every fifteen minutes or so...
Link to comment
Share on other sites

Yes, that's what I meant. The user that is logged in will refresh the timestamp each time they load a page.About other users having to wait, well, that's all you can do if you can only allow one user to log in at a time.

Link to comment
Share on other sites

I still don't see the purpose of it, except an irritation for users that need to edit and run.Agh, It doesn't matter it's created so if (it works) Great! }else{ too bad!}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 userajaxCall(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 6else 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;}}  }  }

Edited by student101
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...