Monitor user time on site

Asked

Viewed 61 times

0

i can monitor how long a user is logged in on a specific page? Saving in the database the time he has been.

The leadership here wants to know if the leaders are using a reporting service provided by us, I already monitor the date and time they access, have any php or javascript function that does this? I didn’t find anything like it

  • You define some kind of setInterval Every minute you can make a request to the server, adding in 1 minute the backend. It won’t overload your server, from what I’m seeing, otherwise I’d recommend something else.

  • you say do a javascript doing an update every 1 minute? How would I catch the time? Or simply do a sum of 1 in 1?

  • every minute of your setInterval can make a call to your server, like: fetch('api.com/increaseTime') the route increaseTime will take from the bank, how many minutes have you saved, add with 1, and save.

  • you can by an example script as response?

1 answer

0


can you put an example script as an answer? - Ricardo Gonçalves

I haven’t touched Mysql for a while. But the query would look like this.

UPDATE table SET field = field + 1 WHERE id = {ID_DO_USUARIO}

In frontend just pass the user ID to back

setInterval(() => {
  let id = user.id
  fetch(`http://api.com/increaseTime/${id}`)
    .then(res => res.json())
      .then(json => console.log(json))
}, 1000 * 60)
  • I couldn’t understand your code

  • You didn’t understand because you had no contact with the fetch ?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.