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 7088039
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:44:55+00:00 2026-05-28T07:44:55+00:00

When user closes the browser and the session gets killed, I need to update

  • 0

When user closes the browser and the session gets killed, I need to update my database of current users. So I have set up a simple cron job that deletes the row.

DELETE FROM visit WHERE unix_timestamp(time)<unix_timestamp(now()-600) and sessionid <>(LIST OF SESSIONS ..need help)

I can get the session files thanks to this code provided here on StackOverflow(thanks)

$sessions = array();

$path = realpath(session_save_path());
$files = array_diff(scandir($path), array('.', '..'));

foreach ($files as $file)
{
$sessions[$file] = unserialize(file_get_contents($path . '/' . $file));
}

echo '<pre>';
print_r($sessions);
echo '</pre>';

My question is are those files returned by the second half of the code the list of active sessions? Basically I’m trying to verify that all the rows that will be deleted will contain sessions that are not part of the “ACTIVE’ list supposedly returned by unserialize(file_get_contents($path . ‘/’ . $file));

I have read that another approach would be to check the last activity timestamp (if it’s greater than say 10 minutes). But I don’t know how to record “last activities’ onto the database.

Thanks

  • 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-05-28T07:44:55+00:00Added an answer on May 28, 2026 at 7:44 am

    Whoa!

    Reference counting is not a good way to keep track of sessions – particularly if the sessions are stored in a completely separate substrate. Trying to maintain a list of the active sessions seperate from the active session list is a redundancy.

    I’d start again. Use a db-bound session handler. It’s up to you if you allow sessions older than gc_maxlifetime to be reloaded (default handler does allow this).

    Then (e.g. treating expired sessions as no longer active):

    SELECT COUNT(*)
    FROM sessions
    WHERE last_updated>(UNIX_TIMESTAMP() + ' . ini_get('gc_maxlifetime') . ')
    

    My question is are those files returned by the second half of the code the list of active sessions?

    As per above, that depends on your definition of active. Some of these will be older than gc_maxlifetime – but (using the default handler) will still be usable.

    If you’re not bothered about reuse of stale sessions/reuse of the same login details, counting the files in session_save_path() will probably be faster – but the DB approach is more accurate and flexible.

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

Sidebar

Related Questions

I have one user who gets an error message when he closes his browser.
My php session is set to expire when user closes the browser but I
By default Flask ends a session when the user closes the browser. How can
Normally a php session expires when the user closes the browser. Is is possible
What happens if the user closes their browser while a called business service method
I need to keep the session live unless until the user clicks logout in
I have an website. When the user is logged the session details will loaded.
when the user closes his browser, usually he losts the cookie that marks the
Is there any way of knowing if the user closes a tab in a
I like to run some code when a user closes my ms 2000 app.

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.