Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9087793
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T21:46:43+00:00 2026-06-16T21:46:43+00:00

so im trying to have a table that shows which users are online on

  • 0

so im trying to have a table that shows which users are online on my site I have everything working besides the part where if someone closes the site without logging out first they stay on the table as logged in. i’ll try to eplain my situation the best I can.

in my database I have a users table in that table is online when you login your online status is set to 1, when you click logout online is set to 0. here is the code im trying to use so that if someone is inactive for 10minutes they will be logged out ( online set to 0 )

<?php
session_start();
$timeout = 10; // Set timeout minutes
$logout_redirect_url = "logout.php"; // Set logout URL

$timeout = $timeout * 60; // Converts minutes to seconds
if (isset($_SESSION['last_activity'])) {
    $elapsed_time = time() - $_SESSION['last_activity'];
    if ($elapsed_time >= $timeout) {
        mysql_query("UPDATE users SET online = 0 WHERE username = '".$_SESSION['username']."'")
        or die(mysql_error());
        session_destroy();
        header("Location: $logout_redirect_url");
    }
}
$_SESSION['last_activity'] = time();
?>

This works fine it logs them out if they wait the 10minutes, but if they just close the browser it keeps online = 1 which displays them on the list.

I believe the problem is that when they close the browser that destroys the session before it gets the chance to update the users online and set it to 0

is there anyway around this? help would be very much appreciated thanks 🙂

btw I know not to use mysql anymore and everything, I will try to switch to something else once I learn more and get everything working first.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T21:46:44+00:00Added an answer on June 16, 2026 at 9:46 pm

    The underlying problem is that when a user closes their browser, there’s no reliable way to first communicate with your server. [There are things you could try, but in my opinion that’s a rabbit hole you shouldn’t go down] All you really “know” is how recently the user visited your site logged-in. (in your case $_SESSION['last_activity']).

    At a basic level, you will want to start persisting that information in your database, and then have a process kicked off occasionally to cleanup stale sessions.

    e.g.

    //On page load with a logged-in user
    mysql_query("UPDATE `users`
                 SET `last_activity` = '" . time() . "' 
                 WHERE `username` = '" . mysql_real_escape_string($_SESSION['username']) . "'");
    

    note that assumes you’ve added an INT column to your table named last_activity

    and then in a process that executes, say, every 15 minutes:

    mysql_query("UPDATE `users` 
                 SET `online` = 0 
                 WHERE `last_activity` < '" . strtotime('-30 minutes') . "'");
    

    Unix servers have something called cron especially for period tasks/scripts, so that’s what I would recommend for kicking off your cleanup script.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a data.frame and I'm trying to create a frequency table that shows
I'm creating a table that shows all the registered users to which the current
I'm trying to normalize a mysql database.... I currently have a table that contains
I'm trying to have a simple html table, that highlights a row as a
I have a heap based table in MySQL that I am trying to update
I have a MySQL table containing a listing of events that I am trying
Here's what I am trying to do. I have a website that lets users
I have a table of items that users have bought, within that table there
I'm trying to create a leaderboards page which shows the top 10 users depending
I have several tables that I am selecting data from. I am trying to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.