Jump to content

PHP Online Users question


iChriss

Recommended Posts

Hello. I have recently been building a user system and trying to code an "Online Users" script for it however it is not working out for me, so I thought I'd come and ask here.

 

I need a script that adds them to the table 'online' when they login and then remove them when they logout. That part is simple to do however I am using sessions and I am trying to figure out a way to check if they are inactive like every 10 minutes, and if they are delete their row from the db.

 

It'd be appreciated if someone could set up a code for this.

Link to comment
https://forums.phpfreaks.com/topic/228164-php-online-users-question/
Share on other sites

This is simple todo, you could even do it simply from your user database, by adding a new field called 'online' set it to 'Yes' when they login and 'No' for anything else.

 

Then retrieve all users that have 'Yes' for that field and that will give you the online users.

 

To log someone 'out' if they become inactive, you could use a Cron-Job to update the database say every 5 minutes to check for people who not done anything in the past 10 minutes, if they haven't set 'Online' to 'No' then when they are active again, if 'Online' equals 'No' log them out and say why, makes their account safer incase they forget to logout or whatever.

 

Hopefully this is something your looking to?

 

Regards, PaulRyan.

There's lots of different ways to attack this problem.  Doing a custom session save handler is one.  You write the session data into a mysql table rather than the default, and once you commit to doing that, you can modify the routines slightly so they do a couple of extra things: 

 

-Include the user id when the user authenticates

-Add a bit of code to your custom session read() function that updates a timestamp column in the session row, you can name something like "last_seen". 

 

If you read the http://php.net/manual/en/function.session-set-save-handler.php comments there are several that include implementations you can modify slightly to do this.

 

Once done, you have an intrinsic mechanism (the last_seen timestamp) that you can query using the mysql date/time functions to get a list of people who have actually done something on the site within the timeframe you decide indicates that they are actively logged in.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.