Jump to content

javascript/php problem on browser close/unload event


waqas87

Recommended Posts

i am trying to set user status as offline when user close browser but i am facing a problem ...

i am calling a function when body unload which will set guest user status to "0" when user close the browser but it not working ...

it execute php before function is called..

any idea why this is happening ...plz help it's making me crazy  :facewall:

 

Link to comment
Share on other sites

You may be better of just recording a users timestamp when they access a page while logged in, and track that in your database.  After, say, 5 minutes of inactivity, you could consider them offline.  This is how most online/offline schematics work.

 

As far as I know there is no way to force a log off when a user closes a browser by simple javascript.  Feel free to prove me wrong on that, though :)

 

One method that does come to mind though would be establishing a simple AJAX 'ping' of sorts.  While a user is logged in, you could be constantly pinging the server with a simple PHP file executed though AJAX.  If the ping occurs every 10 seconds, you could consider any 'most recent ping' of a user that is more than 10 seconds ago to be indicative that they have either closed the browser, navigated away from the site, or logged out.

 

I'm curious to see the resolution of this thread, though...

 

Good luck,

Theo

Link to comment
Share on other sites

sorry about here it is

        {

               
                        <?php
                                        $Gueststatus="update tbl_guestuser set STATUS='0' Where USERNAME='$userG'";
                                        mysql_query($Gueststatus);

                                        $Userstatus="update tbl_signup set loginststus='0' Where MemberUserName='$user'";
                                        mysql_query($Userstatus);
       

                        ?>
                                alert('hi');
                       
                               
        }

Link to comment
Share on other sites

You can't use PHP like that. PHP is a server-side language. It runs the code, sends the output to the browser and has nothing to do with anything after that. nethnet's solution is much better (it actually works :P).

Link to comment
Share on other sites

You don't need to use that AJAX method unless it's necessary that stats are updated that fast, which in most cases it's not. You can just set the timeout to something like 5 minutes. Every time the user requests a page store the time of that request. If the last time they visited the page is over 5 minutes (or whatever you set your timeout value to be) then you can assume they're offline.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.