Dear Friends,

I am following the bestuff.com's active member session.
I am doing the following thing...
1. when user logs in, I update my database's field with value 'O' (means user is logged in and fires query which has flag value 'O')


But the problem is how to check user's status when User closes browser?

I set 10 minutes session time out but it only works when user refreshes or visits the page.

Please help me out of this... hell...

Please reply me fast and please admin section also take part in this query/problem...

Thanks in advance

index.php
----------

session_start();
if (isset($_SESSION['LAST_ACTIVITY']) && (time() - $_SESSION['LAST_ACTIVITY'] > 600))
{
    // last request was more than 30 minates ago
    //session_destroy();   // destroy session data in storage
    //session_unset();     // unset $_SESSION variable for the runtime
    header("location:logout.php");
}
$_SESSION['LAST_ACTIVITY'] = time(); // update last activity time stamp

logout.php
----------

session_start();
include_once 'commoninclude.php';
$objLogin=new MySQLConnection();
$objLogin->Open();
$updateMemberOnlineStatus="Update tblMemberRegistration Set OnlineStatus='F' Where ID=".$_SESSION['memberid'];
$objLogin->UpdateQuery($updateMemberOnlineStatus);
$objLogin->Close();
session_unset();
session_destroy();
header("location:index.php");

but the above code does not working...

please help

Dani AI

Generated

Short answer for : there is no reliable server-side signal that a user “closed the browser.” Client events like beforeunload/unload can help, which is what suggested, but they are not 100% dependable. ’s pointer to custom session storage is useful for design, and ’s SQL quoting note is a good catch for avoiding syntax errors.

A robust, simple pattern that scales and avoids race conditions is to treat “online” as recent activity rather than a forced logout on close. Add a last_activity DATETIME (or keep an active_sessions table keyed by session_id), update that timestamp on each authenticated request (or via a lightweight ping), and render the Active Members list from recent timestamps. Example SQL to fetch active users:

SELECT ID, username, avatar
FROM tblMemberRegistration
WHERE last_activity >= DATE_SUB(NOW(), INTERVAL 10 MINUTE)
ORDER BY last_activity DESC
LIMIT 40;

For faster UX you can try client-side signaling as a best-effort: use navigator.sendBeacon in unload and also run a periodic heartbeat to keep the timestamp current. These are examples only — still keep the server-side timestamp as the authoritative source.

window.addEventListener('unload', function () {
  navigator.sendBeacon('/api/mark-offline.php', '');
});

setInterval(function() {
  fetch('/api/ping.php', { method: 'POST', credentials: 'include' });
}, 60000);

Finally, add a server-side fallback/cleanup: a cron job that marks users offline (or removes stale session rows) if last_activity is older than your threshold. If multiple devices/tabs are possible, track per-session rows and derive a unique-user list for display. Caveats: unload/sendBeacon can fail (crash, network, mobile sleep), multiple open tabs can complicate flags, and PHP’s GC is probabilistic — so rely on last_activity + periodic cleanup as the core solution. Combining client attempts for immediacy with server-side last-activity checks gives the most reliable Active Members list.

Recommended Answers

All 10 Replies

Member Avatar for Member #733618

do this for logout.php

Update tblMemberRegistration Set OnlineStatus='F' Where ID='".$_SESSION['memberid']."'"

Dear P0lT10n,

Update query is executing perfectly when we logout force fully or when browser is opened and session is time out.

My problem is how to logout when user closes browser?

When user closes browser, there should be some code to handle that browser close problem.

Please work out on that.

Thank you for reply.
Sumit Joshi

Hello Sumit,
I don't know the exact solution to you problem but the following link may be useful to you.
Session logout

Dear Chintan,

Thanks for reply.
My problem is when user closes browser, automatically database table must be updated.

My problem is that I am not able to execute query on browser's close event.

Seeking good help.

If you absolutely must run the query when the user closes the browser, you can trigger some Javascript on the close() event for the window. That Javascript can make an AJAX call to a PHP page that does the logout.

However, my question is whether you really need to logout on window close. I presume you're using PHP sessions - which automatically expires when the browser is exited. So, as far as user security, that's handled.

If the database sessions aren't updated until the next page load by someone - what's the harm? Does it really matter that there's an expired session hanging around if no one is accessing the site?

Dear Friend,

Thank you for you time and reply.

See my exact problem
1. visit http://bestuff.com
2. At the last there is section of "Active Members", in which it shows top 40 to 44 users who are online.
3. When I get logged in, I will be shown on that list because I just logged in last.
4. When I close browser without logging out force fully then my profile image will be removed from that section.

If you have any other solution for that please help me that how to do?

Thanks in advance.
Sumit Joshi

Member Avatar for Member #733618

that would be a time out... i dont know how to do that :S

Dear Friends,

Ok I agree with you that it's time out.
But how I able to check from Server that User has session time out and then update the table?

amit if you have set the session time for 10 minute.so then it does't matter that user close the browser. session will automaticaly destroyed after 10 minute.

amit if you have set the session time for 10 minute.so then it does't matter that user close the browser. session will automaticaly destroyed after 10 minute.

Dear Freddavis,

You all right that when browser get closed all session varialbes will be destroyed.

My main and serious problem is how to Update table when browser get closed?

I am finding this answer since long time and have visited most websites and forums but could not found solutions yet.

Is there anybody who can solve this?
Is there any expert in daniweb who can solve my problem?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.