Cleaning Up Ghost Entries in MySQL on PHP Session Destroy/End
Posted on 2007-10-19
I'm working on a chatroom that uses PHP and MySQL and PHP Sessions in the backend.
It works great except for a side effect of HTTP connectionless nature.
Lets use this table: "Users_In_Chatroom"
ChatroomId (int), UserId (int), DateJoined (datetime)
When a user enters the room:
$insert_query=sprintf("INSERT INTO Users_In_Chatroom(ChatroomId, UserId, DateJoined) values(%s, %s, '%s')",
When a user leaves the room
$delete_query=sprintf("DELETE FROM Users_In_Chatroom where ChatroomId=%s AND UserId=%s",
So Far so good.....but when a user simply CLOSES their web browser without purposfully "LEAVE"ing the chatroom, they are left sitting there, forever.
I can have the browser automatically update some field with a datetime that says "I'm still alive" and then respond somehow to (now()-"I'm still alive")>30 minutes, or some timeout, but how would I do that??
I'm on a public server which means no server-side code can be continuously running.
I am however successfully using PHP sessions, that timeout on there own after 30 minutes of inactivity. Perhaps I can some code that executes on session destroy??
I hope someone can help me with this :) Thanks!!