Jump to content

Tracking User/Session Activity


Baez

Recommended Posts

Hello hello,

 

Another new member here as you can see. I'm building a site in which part of the member search requires that the user be online in order for them to be shown. I searched around but did not find anything directly related to my question.

 

I have all DBs, tables, login system etc made already. Once a user has logged in or out I call this function:

 

function setUserOnlineStatus($user, $checklogin)
{
$connect = dbConnect();

$result = $connect->query("update users
			    set online_status='".$checklogin."'
			    where username='".$user."'");

if (!$result){return 1;}
else{return 0;}
}

 

Pretty simple as it just updates the users status. 0 means offline and 1 means online.

 

Here's my problem. If the session ends by the user closing the browser or the session timing out after 30 minutes (I have "session.gc_maxlifetime" set to it in the INI) then the user's online status never changes back to 0.

 

How can I track the activity of a user's session so that if the session ends by not logging out, the setUserOnlineStatus function still gets called and the online status set to 0?

 

Thanks a bunch.

Link to comment
https://forums.phpfreaks.com/topic/181025-tracking-usersession-activity/
Share on other sites

You can go about this two different ways.

First assuming in the table there is a field for when the user times out or when theywere last active you could just create a function to cycle through all the rows and change the ones that had a time that was over 30mins old if that makes sense? The only problem is it relys on the users to delete other users and it might slow down the site for them if you have alot of users.

Second use a crontab to cycle through the table and do what I explained above but instead have the server do it automatically every ___ minutes or hours or days. You can set it to run the script every 1minute if you want.

 

I hope that made sense haha

Hehe yes it does thank you very much. I have another crontab script already running and was thinking that was best way to do what I wanted to. Every 5 minutes maybe?

 

However I don't have a field for last activity and that's probably more of my question now that I know how to go about implementing it. Adding the field isn't a problem. How would I go about tracking the user and sending the "last activity" time to the database?

Well how I would go about it is have a start_time, last_active kind of set up.

so when they log in or visit for the first time it records the time the session was started and this is nice so you know how long users stay on your website. then everytime they go to a new page you have a small piece of code update the last_active column. then when you use cron tab, just look for ones that are more the 30mins old.

 

You just create a function to update the row

you can relplace the ip with whatever makes the user unique

$time=time();
$Query="UPDATE `table` SET `Time_Update`=$time WHERE `ip`=$IP";

Then if they are a new user you insert a new row instead of update the current one.

Let me know if that works otherwise I can post more examples

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.