Click to See Complete Forum and Search --> : Delete data on browser close.
Blalien
December 22nd, 2008, 07:22 AM
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
PeejAvery
December 22nd, 2008, 07:31 AM
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.
Blalien
December 22nd, 2008, 07:49 AM
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.
PeejAvery
December 22nd, 2008, 09:02 AM
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.
zaryk
January 3rd, 2009, 11:35 AM
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
//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
}
//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
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.