Welcome Guest, Not a member yet? Register   Sign In
Database update on session expire
#1

Hello,

I have an "active" column in my user's table that is updated to "yes" whenever a user is logged in. I can update it back to "no" if the user logs out through the actual /user/logout controller, but if the user just closes their browser (like most do) the session expires automatically but my active column still says they're logged in.

Is there anyway I can set the column appropriately if their session expires by codeigniter?

I am using Codeigniter 2.2.0 and my sessions are being stored in MySQL.

Thanks for the help.
Reply
#2

You could use any number of hacks to update it at some point in the future if it has expired. However, if you instead stored a timestamp whenever you recorded a user's activity, you could just calculate the difference between the most recent timestamp for that user and the current time to determine whether that user is currently active (and avoid making that calculation if they logged out).
Reply
#3

(This post was last modified: 01-28-2015, 01:41 PM by Mel9pr.)

(01-23-2015, 06:53 PM)Jguy Wrote: Hello,

I have an "active" column in my user's table that is updated to "yes" whenever a user is logged in. I can update it back to "no" if the user logs out through the actual /user/logout controller, but if the user just closes their browser (like most do) the session expires automatically but my active column still says they're logged in.

Is there anyway I can set the column appropriately if their session expires by codeigniter?

I am using Codeigniter 2.2.0 and my sessions are being stored in MySQL.

Thanks for the help.

To give you an idea to start with. I think you need to use php to kill all sessions at iddle after some time, let say 20 mins or so, and have your database to delete all sessions records or change your active column records older than 20 mins or so. It can be 10 mins, 20 mins, this time it is really your call. If your site use sessions and the database to keep users logged in this will log out EVERY ONE on your site after 20 mins or so (of course). So how to avoid active users from the 20 mins wipe out. You can use a javascript like this https://github.com/rigoneri/timeout-dialog.js to tell users about their session expiration time and give them a chance to renew their session time to keep them online. Just make sure the javascript update the user session time and the database session time as soon the 'Yes, Keep me signed in' is depressed. As a final note... javascript is a must. Otherwise it will not work and all users not using javascript on their browsers will be logged out after 20 mins so. Hope this help!
Reply
#4

(01-28-2015, 01:25 PM)Mel9pr Wrote:
(01-23-2015, 06:53 PM)Jguy Wrote: Hello,

I have an "active" column in my user's table that is updated to "yes" whenever a user is logged in. I can update it back to "no" if the user logs out through the actual /user/logout controller, but if the user just closes their browser (like most do) the session expires automatically but my active column still says they're logged in.

Is there anyway I can set the column appropriately if their session expires by codeigniter?

I am using Codeigniter 2.2.0 and my sessions are being stored in MySQL.

Thanks for the help.

To give you an idea to start with. I think you need to use php to kill all sessions at iddle after some time, let say 20 mins or so, and have your database to delete all sessions records or change your active column records older than 20 mins or so. It can be 10 mins, 20 mins, this time it is really your call. If your site use sessions and the database to keep users logged in this will log out EVERY ONE on your site after 20 mins or so (of course). So how to avoid active users from the 20 mins wipe out. You can use a javascript like this https://github.com/rigoneri/timeout-dialog.js to tell users about their session expiration time and give them a chance to renew their session time to keep them online. Just make sure the javascript update the user session time and the database session time as soon the 'Yes, Keep me signed in' is depressed. As a final note... javascript is a must. Otherwise it will not work and all users not using javascript on their browsers will be logged out after 20 mins so. Hope this help!

... or you can just use a session only for logged-in users (which is really the purpose of sessions) and let it work naturally.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB