Jump to content

Detect User Clicking Terminate To Log Them Out?


dzhax
 Share

Recommended Posts

The website base I am working with came pre-loaded with isset() php sessionsI am using my database to keep track of who is online or not, but when the user terminates the browser (by hitting the X at the top right) the php session terminates but i need to make my mysql_query(...) run to remove the player from online status.What I really need to know is if there is either a way to catch this a preform the operation before the browser exits, or can I have a timed session that will then allow me to run my mysql_query when the session times out?Thanks in advance.

Edited by dzhax
Link to comment
Share on other sites

I'm sure there will be better answers, but at the very least JavaScript can detect the onunload event, which if set to start a function in the body tag (<body onunload="whatever()"> could POST via AJAX the relevant information to an external PHP document that will handle the db query. It's important to keep the code as short and efficient as possible though, in case the page closes before the script has finished running.

Link to comment
Share on other sites

Javascript has the standard onunload event, and Internet Explorer's onbeforeunload. You may use those if you like, but a server-side solution is required because Javascript won't always work.You should store the online users in an extra database table, an XML file or some other information storage means; with the date that they last visited a page. Every time the page is loaded by somebody, a script checks the list of online users and compares their last connection date to the current date. If the current date is more than five minutes ahead from the other date then you delete the entry and consider the user offline.

Link to comment
Share on other sites

You can also use a custom session handler to store the session data in the database instead of the default location, and manage the sessions yourself. PHP runs a garbage collection function periodically to check for expired sessions and clean them up. You could use that to update your database, or a better solution would be to just count the number of active sessions to figure out who is online instead of keeping track of that yourself. This post has all the information you need to do that:http://w3schools.invisionzone.com/index.php?showtopic=9731

Link to comment
Share on other sites

@chibineku - wont i have to put that function in every page then?@ Ingolme - How would I do the second part of your response? I already have just a table with username in it that is all. I use the mysql_num_rows() to get my players online number.@justsomeguy - Custom session handling sounds like a lot of work for a simple feature. I need to keep this information in my database so i can have player lists and so I can keep track of activity.This is the code i added to my logout.php

$sql = "DELETE FROM playersonline WHERE username = '" . $_SESSION['user'] . "'";	$deluser = mysql_query($sql, $con);	echo mysql_error();	if (!$deluser)		die('Could not remove user from database... - Error 3');

It connects above that is was $con is.

Edited by dzhax
Link to comment
Share on other sites

Is there a way to edit the session_destroy() command to add a mysql statement to delete the user from the database?The onunload and onbeforeunload both did not work.Im just trying to do this the easiest possible way that I will understand how to edit it if need be.

Edited by dzhax
Link to comment
Share on other sites

Why not make a custom function which calls session_destroy(), and then deletes user.

function logout(){  session_destroy();  //mysql code here..}

either stick this in a separate file and include it, or stick it in the actual logout file and just call 'logout();'

Link to comment
Share on other sites

Is there a way to edit the session_destroy() command to add a mysql statement to delete the user from the database?
Yeah, with a custom session handler you can do whatever you want when session events happen.
Why not make a custom function which calls session_destroy(), and then deletes user.
There's no guarantee that the logout function gets called, people just close their browser or go to a new page without explicitly logging out.
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...