PDA

Click to See Complete Forum and Search --> : [RESOLVED] Prevent Window from Closing


_Yoyo
Jun 16th, 2008, 03:15 PM
Hi!

How can I, from PHP, prevent a window to be closed? I mean, if certain condition is met (a cookie exists for instance) write some javascript or whatever to prevent the browser to be closed before some action is executed...


If(cookie)
do not allow window to be closed because session details gotta be saved to a DB before that;
else
Go ahead, close it!;

RudiVisser
Jun 16th, 2008, 03:22 PM
As far as I can see, you answered your own question.. Or do you want an implementation example?

the182guy
Jun 16th, 2008, 03:47 PM
IMO it's a waste of time. Trying to prevent the window closing is just going to annoy users.

Aside from that, even if you are successful using Javascript, some users may be using a browser that does not support Javascript, or they may simply have it disabled. They may even have cookies disabled or blocked for untrusted websites.

I suggest you just use some sort og Logout button, which can direct to a page that executes your set cookie code.

_Yoyo
Jun 16th, 2008, 03:53 PM
IMO it's a waste of time. Trying to prevent the window closing is just going to annoy users.

Aside from that, even if you are successful using Javascript, some users may be using a browser that does not support Javascript, or they may simply have it disabled. They may even have cookies disabled or blocked for untrusted websites.

I suggest you just use some sort og Logout button, which can direct to a page that executes your set cookie code.

The disabling of javascript or cookies is not an issue. I got that covered. My users often careless about buttons, so i'm losing log data everytime they don't use the 'logout' button.

Some working code would be of great help :(

RudiVisser
Jun 16th, 2008, 03:55 PM
Should you care to explain your *actual* issue further, we may be able to assist you in finding a more suited solution.

_Yoyo
Jun 16th, 2008, 04:04 PM
Thanks for answering. Everytime a user log into the system, I record to a DB user, date log in, time log in, date log out and time logout. As you may know by now, when they log in I can record the data, but if they just decide to close the browser ignoring the "log Out" button, date logout and time logout won't be recorded.

penagate
Jun 16th, 2008, 08:08 PM
You can derive the logout time as (last action time + session time-to-live). TTL is arbitrary; usually defined as 15 minutes or thereabouts.

_Yoyo
Jun 17th, 2008, 07:26 AM
Thaks for the response penagate. Would you please give me an example of what you're talking about :(

penagate
Jun 17th, 2008, 08:49 PM
Well, if you record every authenticated page request in a database along with a timestamp, then you shouldn't need an automatic log out facility.

Add this to every public page:
require 'includes/log.php';

log.php:
<?php
# some code to prevent this file from being requested directly
# ...

sql(
'INSERT INTO accesslog (userid, pageuri) VALUES (?,?)',
$userid,
$_SERVER['REQUEST_URI']
);
?>

Something like that. Vary according to taste.

_Yoyo
Jun 18th, 2008, 09:36 AM
Awesome idea! Thanx!!!!