...

View Full Version : Update database on browser exit



jasonmc
11-12-2010, 10:18 AM
I need a bit of advice please...I am working on a chat program. When a user logs in, a value in the database is set to 1 (online), and when he/she logs out it is set to 0 (offline).
The probelm is, if a user closes the browser, he/she is still online; not only this, but the session (variables; $_SESSION['username'] etc) is still active aswell.
I was wondering if there is a way around this problem? I have tried onbodyunload in the body tags but cant get it to work. Somebody has suggested having a timer so it automatically logs out after a set time bu i have not got a clue how to do this. If anybody has any advice, please help me it is driving me mad...thanks you very much in advance for any replies :)

sybil6
11-12-2010, 03:54 PM
Try an Ajax request to the DB on Js unload.

jasonmc
11-12-2010, 05:13 PM
Hi, thanks that was what I was hoping to do but i dont know how to go about doing it, do i put onunload in the html body tags, and point to a js file which points to a php file with the query? Thank you very much for your time :)

sybil6
11-13-2010, 12:39 AM
I tried onunload with ajax and it does not work(the database is not populated) , i'll post later if I find another solution.
Anyway your session should be destroyed if someone quits the browser, maybe your doing something wrong with sessions.

j9sjam3
11-13-2010, 12:58 AM
<body onunload="doUnload()">
...
function doUnload() {
//ajax request to PHP file?
alert("Updated database.\nSorry to see you go!");
}


Maybe that will work?

jasonmc
11-13-2010, 12:03 PM
I tried onunload with ajax and it does not work(the database is not populated) , i'll post later if I find another solution.
Anyway your session should be destroyed if someone quits the browser, maybe your doing something wrong with sessions.

Thanks sybil6...if u find a solution it would be awesome if u let me know...

Thanks also j9sjam3 i have tried something like that...it doesn't work unfortunately, if i have the js function in the header, it sets it to offline automatically, if its external file it does nothing atall :( thanks anyway...if i find a solution i will post it back aswell :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum