golem Posted March 13, 2007 Share Posted March 13, 2007 Hi, How can I run a logout script when a user close the browser. This is for a login/registratin system, to change a flag in teh database from "online" to "offline" if user closes the browser without loging out. Thank you. Quote Link to comment Share on other sites More sharing options...
idevlabsdotcom Posted March 13, 2007 Share Posted March 13, 2007 possibly javascript. php is server side, and has no idea what is happening on the user's end unless it is sent something. i don't do javascript though, so someone else is going to have to chime in. the only way to do it purely in php would be to save a timestamp when someone logs in, and if their session has been inactive for X number of minutes then flag them as logged out. this script would likely be set up to run every X minutes with cron, and will likely hit your database more times than it is worth. it won't have anything to do with the window closing though. if you're interested in this method, a helpful tutorial can be found over at codewalkers.com: http://codewalkers.com/tutorials/14/1.html Quote Link to comment Share on other sites More sharing options...
golem Posted March 13, 2007 Author Share Posted March 13, 2007 Thanks for your answer. If I understand right, if I have a database of 50000 users, I will have to go through all of them periodicaly and compare current time with timestamp stored in the database from last activity? . Quote Link to comment Share on other sites More sharing options...
idevlabsdotcom Posted March 13, 2007 Share Posted March 13, 2007 no, the script that is running periodically to log people out if they are inactive will only need to check those that are already online. there's no point in trying to log someone off that's already offline. Quote Link to comment Share on other sites More sharing options...
golem Posted March 13, 2007 Author Share Posted March 13, 2007 It will still need to go through all the records to check who is online... will that be heavy on server resources, if lets say I want to check every 1 min? Quote Link to comment Share on other sites More sharing options...
interpim Posted March 13, 2007 Share Posted March 13, 2007 hmmm 50k+ queries every 60 seconds... you think it will get done with the first set before it has to start over from the beginning again? Quote Link to comment Share on other sites More sharing options...
legohead6 Posted March 13, 2007 Share Posted March 13, 2007 if you use sessions it automaticly cancels when browse is closed Quote Link to comment Share on other sites More sharing options...
HoTDaWg Posted March 13, 2007 Share Posted March 13, 2007 well if you really think this is important and you have the time, interest, and resources check out AJAX. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.