[RESOLVED] Prevent Window from Closing
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...
Code:
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!;
Re: Prevent Window from Closing
As far as I can see, you answered your own question.. Or do you want an implementation example?
Re: Prevent Window from Closing
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.
Re: Prevent Window from Closing
Quote:
Originally Posted by the182guy
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 :(
Re: Prevent Window from Closing
Should you care to explain your *actual* issue further, we may be able to assist you in finding a more suited solution.
Re: Prevent Window from Closing
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.
Re: Prevent Window from Closing
You can derive the logout time as (last action time + session time-to-live). TTL is arbitrary; usually defined as 15 minutes or thereabouts.
Re: Prevent Window from Closing
Thaks for the response penagate. Would you please give me an example of what you're talking about :(
Re: Prevent Window from Closing
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:
PHP Code:
require 'includes/log.php';
log.php:
PHP Code:
<?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.
Re: Prevent Window from Closing