CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Dec 2008
    Posts
    2

    Question Delete data on browser close.

    Hello,

    I'm building a PHP application at the moment but there something that doesn't work for me.

    The concept is:
    User logs in -> application checks user account in Table1 -> application checks if user is logged in table2 -> application checks ip address in Table2 -> Application inserts ID and IP into table2.

    Table1 contains all the users and user data.
    Table2 contains the users who are logged in at the moment, it contains there ID and IP.

    Normally when a person logs out, the data of the table row will be removed.
    But what can I do about the problem, when people don't use the function log out and close the browser instead? The data keeps existing and the user can't login anymore.

    Is there a good solution for destroying data on browser close?


    Thanks,

    Blalien

  2. #2
    Join Date
    May 2002
    Posts
    10,943

    Re: Delete data on browser close.

    JavaScript fires two events when the browser closes, onunload and onbeforeunload. However, security does not permit that either of those two events allow for redirection of URL data at close. So, the server itself, cannot detect the browser's closing.

    So, you have to implement an activity timeout. For example, create an extra column for the user and name it activity or timestamp. Once the user's last activity is greater than 5 minutes past the current timestamp, delete the data.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  3. #3
    Join Date
    Dec 2008
    Posts
    2

    Re: Delete data on browser close.

    Thanks for replying.

    I have thought about that, but the point is, is that the users will be using the application for about 8 hours each day. And a timestamp will be very annoying in my opinion.

  4. #4
    Join Date
    May 2002
    Posts
    10,943

    Re: Delete data on browser close.

    It may be annoying, but using a timeout it is the ONLY way. There are other ways of making the client communicate. But, either way, you have to have a timeout.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  5. #5
    Join Date
    Apr 2008
    Posts
    214

    Re: Delete data on browser close.

    I am having the same problem. And until i read this thread, I hadn't put much thought into the fix for it. I have an idea, though, depending on how many people you have logged in it might slow the page down a bit.


    This is just an idea, haven't really felt like put the code together.

    Place a code, something like the below, in index.php, under session_start();

    my index.php contains all files necessary
    Code:
    //pseudocode
       mysql_connect
       mysql_select_db "logged in users"
       $ch = mysql_query("SELECT from "logged in users"
       
       while($row = mysql_fetch_array($ch)){
          if(!isset($_SESSION[$row['SessionId']])) 
             delete old-log-in info
       }
    Code:
    //index.php
    
    <?php	
    session_start();
    
    //place above code idea here
    
    	echo "<head>
    		<link rel='stylesheet' href='css/index.css'>
    	</head>
    	
    	<body>
    		<div class='Main'>";
    			include 'Data/login.php';        //header - top
    			include 'navigation1.php';      //navigation - underneath header
    			include 'switch1.php';	      //switch statements - takes care of body
    			include 'usersOnline.php';      //footer - bottom
    		echo "</div>
    	</body>";
    ?>

    So, the bad thing about it, for me:

    1. if 1000 people were, at one time or another, logged in, it would have to check each and everyone
    2. it would check each and everytime someone logged in or clicked on a link

    Good thing, for me:

    1. it would be road kill on deleting old log-in info
    Last edited by zaryk; January 3rd, 2009 at 12:54 PM.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured