SitePoint Sponsor

User Tag List

Page 1 of 2 12 LastLast
Results 1 to 25 of 27
  1. #1
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to know browser is closed or not?

    Hey all,

    I have user management system in my site. One user logs in and his status goes to be Online (1) in the database and this user will be seen as Online in other user's browser. And there is no problem if the user logs out manually then it is fine i can set his status to Offline (0). But when the user closed browser without logging out properly, then how can i know the user is not Online?

    So is there any event that i can track the user is closing the browser so that i know the user is not more online?

    Thank you in advance.
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  2. #2
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to know browser is closed or not?

    Hey all,

    I have user management system in my site. One user logs in and his status goes to be Online (1) in the database and this user will be seen as Online in other user's browser. And there is no problem if the user logs out manually then it is fine i can set his status to Offline (0). But when the user closed browser without logging out properly, then how can i know the user is not Online?

    So is there any event that i can track the user is closing the browser so that i know the user is not more online?

    Thank you in advance.
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  3. #3
    SitePoint Guru mmarif4u's Avatar
    Join Date
    Dec 2006
    Location
    /dev/swat
    Posts
    619
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I find this some where esle and find it very useful, may be it will give some idea.

    Try creating a special column named something like, `user_time`. At the top of every page you can run a statement such as:

    PHP Code:
    mysql_query('UPDATE `users` SET `user_time` = UNIX_TIMESTAMP() WHERE `user_id` = "INSERT AN ID"'); 
    This will in turn update their time every time they load a page. You can then use something like:

    PHP Code:
    mysql_query('SELECT `user_time` FROM `users` WHERE `user_id` = "INSERT AN ID"');

    That will select the users time from the db for whatever user you choose, then if you take that timestamp and compare it to the current time you can see if they have been online in the past 10 minutes.

    Something like this will do

    PHP Code:
    if ($user_time time() - 900) {
        echo 
    'The user has loaded a page in the last 15 minutes.';
    } else {
        echo 
    'The user has not loaded a page in the last 15 minutes, they probably closed the window.';


  4. #4
    SitePoint Wizard DougBTX's Avatar
    Join Date
    Nov 2001
    Location
    Bath, UK
    Posts
    2,498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could do something in onunload, but the best way would just be to track inactivity - if they don't request a page for say 5 to 10 minutes, update their status to offline. One way you could do this, would be instead of recording an "online" or "offline" status in the database, you could store a "last seen" time. Then whenever they visit a page you could update this time, and whenever you want to know if someone is "online" or not, you just need to compare that time with the current time, and see if they are more than 5 to 10 minutes apart.

    hth,
    Douglas
    Hello World

  5. #5
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think i could make you all clear about my problem. Or might be i could not what you guys are trying to feed me. lol.

    I will be able to find whether a user is online or not with the flag that i have set in the database table. But here i want is to set the user's status as Offline if the user did not click on the Logout button/link instead he just closes the browser directly. In that case how can i track?
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  6. #6
    SitePoint Zealot Servyces's Avatar
    Join Date
    Jun 2007
    Location
    The Netherlands
    Posts
    112
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    As suggested by DougBTX, you can try using the onunload html option to execute a piece of PHP code when the page is closed.

    More info about that can be found here.
    Last edited by Servyces; Jun 13, 2007 at 09:24.
    Servyces.com
    Where itís all about you.
    Your partner in online solutions.
    Visit our website at http://www.servyces.com/

  7. #7
    SitePoint Addict chartahir's Avatar
    Join Date
    Oct 2006
    Location
    Karachi, Pakistan
    Posts
    253
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i've done that thing but using ajax, if you wana try it, i'll give it to you, but the only limitation is that , if the JS is disabled this thing doesn't work.
    chartahir

  8. #8
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Servyces:
    As suggested by DougBTX, you can try using the onunload html option to execute a piece of PHP code when the page is closed.
    If i use onUnLoad event of the body, then it will make the user offline or logout when the user goes to another page of the site. But i want the user to be online once logged in until he/she logs out manually or closes the browser.

    chartahir:
    i've done that thing but using ajax, if you wana try it, i'll give it to you, but the only limitation is that , if the JS is disabled this thing doesn't work.
    Yes you can give me a piece of code that you used. I dont care about whether the user has enabled the JS in his/her browser or not.
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  9. #9
    SitePoint Addict aniltc's Avatar
    Join Date
    Nov 2006
    Location
    INDIA
    Posts
    399
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi chartahir

    Could you please explain how it is possible through AJAX and php

    Thanks

  10. #10
    SitePoint Wizard bronze trophy devbanana's Avatar
    Join Date
    Apr 2006
    Location
    Pennsylvania
    Posts
    1,736
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I wouldn't bother with onUnload, just do what was suggested and use the last time the user accessed a page. if it is older than some threshold, then assume they are not online anymore. that's probably the most reliable way of doing it.

  11. #11
    SitePoint Addict chartahir's Avatar
    Join Date
    Oct 2006
    Location
    Karachi, Pakistan
    Posts
    253
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    First Step

    Add this code in head section ,

    Code:
    <script src="logoff.js"></script> 
    
    <?php 
    
    echo " <script language='javascript'>";
    echo "var temp = '".$_SESSION['test']."';";
    echo "</script>";
    
    ?>
    
    
    <SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">
    this.onunload = function() { OnCloseWindows(this); return 0; };
    
    		function OnCloseWindows(win)
    		{
    		
    		//temp is the user_id, this is used to loggof user.		
    		ugdisplay(temp);
    		
    		}
    
    </SCRIPT>
    chartahir

  12. #12
    SitePoint Addict chartahir's Avatar
    Join Date
    Oct 2006
    Location
    Karachi, Pakistan
    Posts
    253
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Second Step

    Now save this code in a file name logoff.js
    Here the user_id is received in function ugdisplay() with variable name str.

    If the user status doesn't change try changing the

    var url="logoff.php"

    with

    var url="http://www.yourdomain.com/logoff.php"

    Code:
    var xmlHttp;
    
    function ugdisplay(str)
    {
    
    xmlHttp=GetXmlHttpObject()
    if (xmlHttp==null)
    {
    alert ("Browser does not support HTTP Request")
    return
    } 
    var url="logoff.php"
    url=url+"?mid="+str
    url=url+"&sid="+Math.random()
    alert (url);
    xmlHttp.onreadystatechange=stateChanged 
    xmlHttp.open("GET",url,true)
    xmlHttp.send(null)
    } 
    
    function stateChanged() 
    { 
    if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
    { 
    document.getElementById("txtHint").innerHTML=xmlHttp.responseText 
    } 
    } 
    
    function GetXmlHttpObject()
    { 
    var objXMLHttp=null
    if (window.XMLHttpRequest)
    {
    objXMLHttp=new XMLHttpRequest()
    }
    else if (window.ActiveXObject)
    {
    objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
    }
    return objXMLHttp
    }
    chartahir

  13. #13
    SitePoint Addict chartahir's Avatar
    Join Date
    Oct 2006
    Location
    Karachi, Pakistan
    Posts
    253
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Third Step

    Now save this file with name logoff.php or you may change the name of the file and name of file in logoff.js.

    Change the database and table info according to yours.

    Code:
    <?php
    
    DEFINE ('DB_USER', 'root');
    DEFINE ('DB_PASSWORD', '');
    DEFINE ('DB_HOST', 'localhost');
    DEFINE ('DB_NAME', 'yourdb');
    
    // Make the connection.
    $dbc = @mysql_connect (DB_HOST, DB_USER, DB_PASSWORD) OR die ('Could not connect to MySQL: ' . mysql_error() );
    
    // Select the database.
    @mysql_select_db (DB_NAME) OR die ('Could not select the database: ' . mysql_error() );
    
    
    
    if ($_GET['mid'])
    {
    $mid = $_GET['mid'];
    
    
    $query = "Update manager set Status = 'Logoff' where ManagerID = $mid";
    $result = mysql_query($query) or die(mysql_error());
    
    echo "result";
    
    }
    
    ?>
    chartahir

  14. #14
    SitePoint Addict eanimator's Avatar
    Join Date
    Sep 2005
    Posts
    396
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by mmarif4u View Post
    I find this some where esle and find it very useful, may be it will give some idea.

    Try creating a special column named something like, `user_time`. At the top of every page you can run a statement such as:

    PHP Code:
    mysql_query('UPDATE `users` SET `user_time` = UNIX_TIMESTAMP() WHERE `user_id` = "INSERT AN ID"'); 
    This will in turn update their time every time they load a page. You can then use something like:

    PHP Code:
    mysql_query('SELECT `user_time` FROM `users` WHERE `user_id` = "INSERT AN ID"');

    That will select the users time from the db for whatever user you choose, then if you take that timestamp and compare it to the current time you can see if they have been online in the past 10 minutes.

    Something like this will do

    PHP Code:
    if ($user_time time() - 900) {
        echo 
    'The user has loaded a page in the last 15 minutes.';
    } else {
        echo 
    'The user has not loaded a page in the last 15 minutes, they probably closed the window.';


    How can I execute this. as user didnt manually log out and just closed the browser.. Session is destroyed.. Shoud I read session file? Problem is USER ID also vanishes. Any IDEA?

  15. #15
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello chartahir,

    Thanks for the code that you have given step by step. But have you tried your code for the purpose in Firefox tab close? And these days IE7 has also tab system. So i have got problem when the user closes the tab last time. I have not tested your code in the tab, have you tested it yet in the tabs?
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  16. #16
    SitePoint Zealot
    Join Date
    Mar 2007
    Posts
    196
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    All you have to do is on every page see if they have a session, if they don't create one and in a database put their last active time, every time they go to a page update the last active time. In case they closed the browser if there is no session make the last database entry for them have a sign out time which will be there last active time then log them in with a new last active time. This will give you a log of users time and you can also track other data with it such as user agent and ip. Now about the online/offline thing, run a cronjob every 5 or 10 minutes on your server that will log users out if there last active time was over 5 or 10 minutes ago. Now you can automatically do this and you don't have to mess with any JavaScript. I hope this helps. This is the method I have chosen and it works fine.
    Kayzio - We don't hesitate, we accelerate.

  17. #17
    SitePoint Guru mmarif4u's Avatar
    Join Date
    Dec 2006
    Location
    /dev/swat
    Posts
    619
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    @ eanimator

    Look at the code carefully when a user go to the page this query will insert his/her record to the db.Now if he/she close the browser than his last time was that which that page enter to the db.In the last code when You run that code it will compare the time with that user time and current time it will show you that the user is in or out. Now it depends on you how much time u want to give and check the user status.

    @ kayzio

    Your idea is also look nice.

  18. #18
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,875
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    There is no way to tell if a user has forgotten to log out. All such systems rely on when the person last accessed a page while logged in and usually use a period of five or ten minutes as the range in which they will consider the person to still be logged in (as described above).

    If you want it to be more accurate, a lot more complex, and enormously more resource intensive then you could use an Ajax heartbeat script to signal the server every so many seconds while they are logged in and automatically log them out when the heartbeat signal is not received. This makes the code about 5000&#37; more complicated and maybe a minute or two more accurate in determining the time they logged out.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  19. #19
    SitePoint Addict aniltc's Avatar
    Join Date
    Nov 2006
    Location
    INDIA
    Posts
    399
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by rajug View Post
    Hello chartahir,

    Thanks for the code that you have given step by step. But have you tried your code for the purpose in Firefox tab close? And these days IE7 has also tab system. So i have got problem when the user closes the tab last time. I have not tested your code in the tab, have you tested it yet in the tabs?

    how can i check whether a tab is closed or not ?

    Do u have any idea?

  20. #20
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    aniltc:
    how can i check whether a tab is closed or not ?
    Do u have any idea?
    Are you asking that question? I am also looking for the event that the window close or tab close occurs.

    Anyway with the little bit differently with AJAX as felgall said i solved the problem. But still if there is any event, i want to know that.
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  21. #21
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,875
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    There is no event when the window or tab is closed because there is no longer a web page in a browser window or tab to run any code for the event.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  22. #22
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not sure whether you can do this with PHP but with ASP and ASP.Net, you can put code in the Global.asa(x) file that will run when the users Session expires.


  23. #23
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The web is stateless. There is no such thing as 'being online'. You can emulate it, but it's merely an illusion. You can of course get this illusion pretty realistic, but you would be better off spending your time understanding why you can't do this, than trying to work around it.

    </rant>

  24. #24
    SitePoint Addict aniltc's Avatar
    Join Date
    Nov 2006
    Location
    INDIA
    Posts
    399
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I agree with you.But its possible with ajax . is n't it?

  25. #25
    SitePoint Wizard TheRedDevil's Avatar
    Join Date
    Sep 2004
    Location
    Norway
    Posts
    1,198
    Mentioned
    4 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by aniltc View Post
    I agree with you.But its possible with ajax . is n't it?
    You can in teory use onunload together with a check where the mouse pointer is. But this does not really work too well, browsers does handle onunload differently etc.

    In addition the mouse pointer can be at the page when I close the window if I use hot keys, which would not trigger the javascript event.

    Hence you are better off simulating the online users after the time they last accessed the site. I.e. do a "select members from table where active last 10 min", when you do that there is no need to remove a user from the table etc, since you just pull the users who was active the last xx min.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •