UPDATE on close

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • zorgi
    Recognized Expert Contributor
    • Mar 2008
    • 431

    UPDATE on close

    Hello people

    I have a situation where i need to update database on user closing browser window. What would be the best way of approaching / solving this problem.

    Thank you
  • nathj
    Recognized Expert Contributor
    • May 2007
    • 937

    #2
    That's a tricky issue. As far as I am aware there is no way to reliably detect a browser close. You see browser activity is detected client-side and your update is done server side.

    If the user closes the browser there is no client-side and so you have no way of detecting any thing as there is no client side to run scripts to tell your server side scripts to perform an update.

    In a nutshell what you have asked can't be done.

    So, what is the point of this update on quit idea? There is, as my old mum says, more than one way to kill a pig/skin a cat. Let us know what the point of this is and we may be able to offer an alternative idea for you to explore.

    Cheers
    nathj

    Comment

    • zorgi
      Recognized Expert Contributor
      • Mar 2008
      • 431

      #3
      Originally posted by nathj
      That's a tricky issue. As far as I am aware there is no way to reliably detect a browser close. You see browser activity is detected client-side and your update is done server side.

      If the user closes the browser there is no client-side and so you have no way of detecting any thing as there is no client side to run scripts to tell your server side scripts to perform an update.

      In a nutshell what you have asked can't be done.

      So, what is the point of this update on quit idea? There is, as my old mum says, more than one way to kill a pig/skin a cat. Let us know what the point of this is and we may be able to offer an alternative idea for you to explore.

      Cheers
      nathj

      First of all thanx nathj.

      The problem:
      I have a page that can only be viewed by one account/user of the group. There is unlimited number of groups but within a group only one user can see the page at the time. I was going to record user details into database when he logs in and basically stop anyone from his group to log in at the same time. However if he doesn't log off but instead closes the window,...well I have a problem, because database has not been updated. I think I have to skin this cat with user activity checks and/or time limits, of course that's unless someone comes forward with elegant solution.

      Comment

      • nathj
        Recognized Expert Contributor
        • May 2007
        • 937

        #4
        I see, this cat is wriggling around and refusing to be skinned. Well, before I stretch the analogy, or the cat, to far I'll press on with what may be a workable idea.

        I should say before outline this that it probably has more holes than a tea strainer.

        Here's my idea:

        1. User 1 attempts access the restricted page and you check in the DB and see that he can, so you store this fact and some indicator the session or cookie.

        2. User 1 shuts the browser and goes to find a cat or make a cup of tea or something. most importantly he does not log out

        3. User 2 attempts to access the restricted page so the PHP check sees that there is some sort of cookie/session ID listed and so attempts to reach that session to see if it's active. If it is active the page is locked if it's not active then the DB is updated to reflect user 1 disappearing and user 2 appearing.

        Does that make sense? Now, how to achieve all that? Also, there are potential issues with session timeouts and stuff, but I still think it is worth pursuing a bit further.

        I hope that helps a bit more, and I promise to stay away from cats everywhere.

        Cheers
        nathj

        Comment

        • zorgi
          Recognized Expert Contributor
          • Mar 2008
          • 431

          #5
          I'll your this apptoach, will see where it takes me and rusalt will post here.

          Comment

          • didoamylee
            New Member
            • Nov 2008
            • 16

            #6
            You may try the "onload" attribute of the html body tag to trigger an AJAX event or you can use cron jobs ( in your problem might not help much ).

            Comment

            • Atli
              Recognized Expert Expert
              • Nov 2006
              • 5062

              #7
              Hi.

              You could try using JavaScript timers and AJAX to keep track of the user while he browses you pages.

              That is; you could set up a database that keeps track of which user is logged in that has a very very short time-out period, after which the user is considered inactive and logged out.
              That timeout could be something like 30 seconds.

              On the page, you could have a JavaScript timer event that sends an AJAX request to a "refresh" page, that would update the database in order to keep the user "active". You could call it like every 15 seconds just to make sure that the user doesn't get "lagged" out.

              This way, if the user closes his browser, the AJAX won't fire, the database won't be refreshed and the user would time-out and become inactive, allowing other users to log in instead.

              Comment

              • zorgi
                Recognized Expert Contributor
                • Mar 2008
                • 431

                #8
                Thank you all for your input. This is what I did.

                First I tried nathjs approach with sessions. However what I wrote was messy so before finishing it I switched to AJAX approach.

                Basically this is my body tag for the page:

                [HTML]
                <body onbeforeunload= "ajax_start (); alert('Click OK to log off ');">
                [/HTML]

                ajax_start() is my function that starts ajax that calls PHP script that updates the database (logs user off).

                alert('Click OK to log off '); is not here just to look good but to give ajax enough time to update the database. Without it database doesn't get updated.

                Comment

                Working...