This topic is locked

Sort of heartbeat

8/9/2020 4:54:45 PM
PHPRunner General questions
A
asawyer13 authorDevClub member

As my user moves around my site, I'd like to update their user record in the database with the date/time.. Basically a form of heartbeat so I know the user is still doing work, and I could use that info to get a pretty good idea of how many users are in my web app at a given time.
Do I put the code in the header or footer??? or where is the best place so it runs on each page refresh??? I might also want to put in other places so if they are on a screen that does searching, it may not refresh the page as they aren't actually moving from one page to another, but I can handle that with no problem.
Alan

A
AlphaBase 8/9/2020

Alan,
Maybe the "Before display" event

D
david22585 8/9/2020

Before display on each page, add code like this:



DB::Query("UPDATE users SET activity=now() WHERE id = ".$_SESSION["user_id"]."");


Be prepared for a lot of writes. If you want to make it a little more complex, you could give each page it's own individual ID, and do something like this:



DB::Query("UPDATE users SET activity=now(), page_id="1", page_activity="1" WHERE id = ".$_SESSION["user_id"]."");


Then you could have page_id 1 be their account information. 2 could be some other table, 3 another table, etc.
Then page_activity 1 could be the list page, 2 be the add page, 3 be the edit page, 4 be the view page, 5 be the search page, etc.
This way you know what table/page they were on last.

aadham 8/10/2020



As my user moves around my site, I'd like to update their user record in the database with the date/time.. Basically a form of heartbeat so I know the user is still doing work, and I could use that info to get a pretty good idea of how many users are in my web app at a given time.
Do I put the code in the header or footer??? or where is the best place so it runs on each page refresh??? I might also want to put in other places so if they are on a screen that does searching, it may not refresh the page as they aren't actually moving from one page to another, but I can handle that with no problem.
Alan


Hi

This will give you who/how many users are logged in at any given time, but it won't tell you at which page users are or what they're doing.
Hope this helps.

Sergey Kornilov admin 8/10/2020

Using AfterAppInit event or header file is the best option. This way you can only add your code to a single place instead of multiple event.
As mentioned previously, this article can help:

https://xlinesoft.com/blog/2015/04/21/displaying-a-list-of-users-that-are-currently-logged-in/