Home  >  Article  >  Database  >  ## How to Determine User Online Status with Sessions and MySQL?

## How to Determine User Online Status with Sessions and MySQL?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-10-26 11:42:29924browse

## How to Determine User Online Status with Sessions and MySQL?

Determining User Online Status with Sessions and MySQL

To ascertain whether a user is actively engaged, one can utilize session tracking mechanisms in PHP and MySQL. Whenever a user accesses a webpage, a timestamp representing their last active time is recorded in their user database entry. Subsequently, a query is executed to count users with last active times within the last five minutes, effectively discerning online users from offline users.

Continuously Updated User Activity

To monitor user activity even during extended periods of inactivity, JavaScript can be employed to send periodic "pings" to the server every 60 seconds. This continuous update ensures that user records remain accurate, regardless of browsing frequency.

Original Code (2009)

<code class="javascript">var stillAlive = setInterval(function () {
    /* XHR back to server
       Example uses jQuery */
    $.get("stillAlive.php");
}, 60000);</code>

Updated Code (2022)

<code class="javascript">(async function ping () {
    // Asynchronously call stillAlive.php
    await fetch( "stillAlive.php" );
    // Issue this call again in 60 seconds
    setTimeout( ping, 60_000 );
}());</code>

The above is the detailed content of ## How to Determine User Online Status with Sessions and MySQL?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn