Satish Ravipati
Satish Ravipati

Reputation: 1449

How to delete data from database when session dont exists

I'm developing a chat module for my application...

  1. I'm opening a window for users to chat, is there way that when users close the chat window, I can update status of that record...i mean event for the closed browser?

  2. I know that default session time is 24mins, so after 24mins of inactivity, user will be kicked out from the site and will asked to login once again.

How to delete/flush the data in my database, when user has no activity for 24mins (when user is logged out from the session due to inactivity)?

Upvotes: 2

Views: 1365

Answers (4)

Webist
Webist

Reputation: 37

Use a javascript function for the event
window.onbeforeunload = myLogoutFunction;

Note: This javascript will not work on browser crash.

Have a user_log database table and fill the login and "logout" dates in it. When there is a user with not updated logoff date, then you can assume there was something wrong with his connection.

$where = " AND `users_id`='".$response['userfound']['id']."'";
$where .= " AND `logoffdatetime`='0000-00-00 00:00:00'";

After 24min php session is gone (default php.ini settings). It wont be much usable. But you can still save into the user_log table.

You should not need the flush database. Keeping the chat users via database alive is bad idea. Instead use a small file with timestamp in it.

Here some other useful tips Detect if the user coming back without logout

$user_navigates = false;
if(isset($_SERVER['HTTP_REFERER']) && basename($_SERVER['HTTP_REFERER']) != _PAGE)
$user_navigates = true;

save also page refreshes into session

if(isset($_GET['pagerefreshed']))
$_SESSION['pagerefreshed'] = $_GET['pagerefreshed'];

save the logging out user_id into session, so you can use it restore things. For instance no need to reload page.

$_SESSION['loggedout']['user_id'] == $login->user_id

Upvotes: -1

pleasedontbelong
pleasedontbelong

Reputation: 20102

1) use the unload event

2) if you are developing a chat, i guess that you have a periodical function that calls the server constantly to retrieve the messages. Each time this function is called, the inactivity time will be reset, even if the user haven't sent a message. If you want to logout the user when he doesn't write anything in 24min you cant rely on the php sessions.

What you can do is: save in the db, the last time the user wrote a message on the chat, and each time you use your periodical function, validate if the user hasn't wrote anything in the last 24 mins

Upvotes: 1

fabrik
fabrik

Reputation: 14365

Store every chat entry's timestamp in UNIX_TIMESTAMP. When a new chat entry incoming check every entries timestamp where timestamp is smaller than now - 24 mins. Kick users.

Upvotes: 1

AlexJF
AlexJF

Reputation: 886

1) You'll need javascript onUnload event for this one. It'll send an asynchronous query to your webserver, setting the offline status of the user. However, you should not rely solely on this event and also set up the 24 mins auto-offline timeout because it is not guaranteed that the user is using javascript.

2) I think your best option here is running a cron job (every 30 mins or so?) that queries your database, identifies the users whose last activity was more than 24 mins ago and then deletes the associated data.

Upvotes: 1

Related Questions