rascle Posted January 16, 2009 Share Posted January 16, 2009 Hi I have a basic User login on my website using sessions and at the moment i have a Log out Button which breaks the session and it also sends a message to my MySQL DB saying you have been logged out (since other members can see if a member is online or not) but the problem that i have is that some people dont log out they just close the browser so they are still online even though they arent if you get what i mean! and i was wondering if anyone could give me a code or tutorial to make it so when the webpage is closed send the message to the MySQL db saying you have left the building? I was thinking maybe writing something like if the session is broken (when they close the page down) send a message to the DB, does anyone know if this would work? if not could i try and do something by mixing PHP and Javascript (i know one is a server side and the other isnt) but is it possible maybe by using Javascripts onpageexit (or whatever)??? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/141140-solved-log-out/ Share on other sites More sharing options...
premiso Posted January 16, 2009 Share Posted January 16, 2009 Hi I have a basic User login on my website using sessions and at the moment i have a Log out Button which breaks the session and it also sends a message to my MySQL DB saying you have been logged out (since other members can see if a member is online or not) but the problem that i have is that some people dont log out they just close the browser so they are still online even though they arent if you get what i mean! and i was wondering if anyone could give me a code or tutorial to make it so when the webpage is closed send the message to the MySQL db saying you have left the building? I was thinking maybe writing something like if the session is broken (when they close the page down) send a message to the DB, does anyone know if this would work? if not could i try and do something by mixing PHP and Javascript (i know one is a server side and the other isnt) but is it possible maybe by using Javascripts onpageexit (or whatever)??? Thanks You have to set a timeout. If the user has not been active in x minutes set them as being logged out. That is the only "accurate" way to do this. If they browse another page, reset their DB status to online. EDIT: http://www.google.com/search?hl=en&q=users+online+script+php&btnG=Google+Search&aq=f&oq= Some scripts there should have examples of how to do this if you need one. Quote Link to comment https://forums.phpfreaks.com/topic/141140-solved-log-out/#findComment-738739 Share on other sites More sharing options...
Psycho Posted January 16, 2009 Share Posted January 16, 2009 Detecting the closing of the browser tab/window could only be done with JavaScript. Don't know if it will work for both tabs and the window or even how cross-browser compatible it is. So, it could be possible to fire off an AJAX request to update the database. But, a better solution, which I assume most every site uses, is to simply set a timestamp of the last user activity. Then when checking for "online" users query for users who's timestamp is within the last x minutes. Quote Link to comment https://forums.phpfreaks.com/topic/141140-solved-log-out/#findComment-738744 Share on other sites More sharing options...
rascle Posted January 17, 2009 Author Share Posted January 17, 2009 Ok Thanks Quote Link to comment https://forums.phpfreaks.com/topic/141140-solved-log-out/#findComment-739030 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.