hi hope you are all fine.
When user login in my web site the table update in database that the user is online and when they click on logout the table updated in database that the user is offline, it working fine but the problem is that when user login and close the browser without logout their account and my table show that the user is online but i want that when user close the broswer withot logout their account the table updated that this user is now offline tell me how can i do this.

<script type="text/javascript">
    function update(){
        <?php mysql_query("update reg set status = 'offline'") ;?>
    }

    window.unload = update();
</script>

i m doing this but it is not working now tell me the solution waiting for your reply.
thanks in advance.

rayidi commented: Something i checking for. I want it exactly but there might no script for this +0

you cannot run PHP code in a Javascript function. You'll have to make an AJAX call to your logout script.

thanks for your reply can you give the example of ajax code to update table when user close the browser.

i doubt if there's any full proof way of handling the browser close event.. Ideally what should happen is :
1) javascript captures the browser's close event
2) and makes an ajax call to the server for further processing
But a browsers behaviour would be to stop doing anything(stop sending any requests) when its closing.. so you can't be 100% sure. Please correct me if i am wrong

You better use sessions for that.

i found in this sort of situation is to create a session timeout script in a include file that way if even a guest comes onto my site it will run the script to see if any logged in members / visiting guests unix timestamp has expired.

example

<?php
// check active members / guests

$guest = time() - 2 * 60; // 2 minutes
$member = time() - 5 * 60; // 5 minutes

$sql = mysql_query("DELETE FROM active_guests WHERE time_visited < ".$gues);
$sql1 = mysql_query("DELETE FROM active_members WHERE time_visited < ".$member);

// this will check the current UNIX timestamp and compare with the values in each table to see if the session has expired, if so this will delete the rows in the tables


?>

then you can run a function to see if the user is online or offline

example

<?php

// get user online status

function getonlinestatus($uid) {
    $sql = "SELECT username FROM users WHERE id='".$uid."' LIMIT 1";
    $res = mysql_query($sql) or die(mysql_error());
    $rowSEC = mysql_fetch_assoc($res);
    $user = $rowSEC['username'];

    $sql1 = "SELECT username FROM active_members WHERE username='".$rowSEC['username']."' LIMIT 1";
    $res1 = mysql_query($sql1) or die(mysql_error());
    $row1 = mysql_fetch_assoc($res1);
    if ($row1['username'] == "") { return "IM OFFLINE"; } else { return "IM ONLINE RIGHT NOW"; }
}

?>

// then call this php function anywhere on your site IE profile

<?php echo getonlinestatus($row['id']); ?>

This is a Test

I've had some of those issues recently...

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.