Re: Cleaning up automatically when leaving a page
| From: | Phpster | Date: | Wed, 01 Jul 2009 03:27:44 +0000 |
| Subject: | Re: Cleaning up automatically when leaving a page | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-294779@lists.php.net to get a copy of this message | ||
On Jun 30, 2009, at 10:59 PM, Paul M Foster <paulf@quillandmouse.com> wrote:
On Tue, Jun 30, 2009 at 06:38:19PM -0700, Mary Anderson wrote:The js function Onunload() could do that but there is the issue if the user hits the back button, or moves to another page. It's the same issue as the standard logout button. Users don't have to trip this to leave the site, closing the browser or moving to another site creates the same issues. Doing the clean up via a cron is prolly gonna be the best option Bastien Sent from my iPodHi all, I have a php application for which I have a page which creates temporary junk and puts it into a persistent store (in this case a postgres database, but that is beside the point.) I have a Save button which puts the stuff I really want into the persistent store and cleans up the temporary junk. I have a Cancel button which only cleans up the temporary junk. I would really like to have the cleanup code run anytime a user leaves the page without hitting the Save button. Is there anyway to do this? I.e. have php code called conditionally on exiting the page?If the user hits the "Back" button or exits via clicking on some link on the page, there's no reasonable way to do this (Michael's cron versions excepted). Because the HTTP protocol has no effective memory, there's no way for it to know that the user exited without saving. You can do various tricky things like set a $_SESSION variable, and then have it checked at the top of each page where the user could go, and check and clean up if the variable has a certain value. But that's pretty tedious. You could execute the whole thing as part of a class, and put a check in the constructor of the class (where it will be called on every page) which cleans up if necessary. In other words, there are ways to do this, but they are tedious. The PHP/HTTP execution engines have no way of doing it on their own. (Oh, there may be a way to do this with Javascript. I'm not good with Javascript, but maybe there's some "OnExit" event which you could use to cause a Javascript function to do an AJAX call to a PHP script which cleans up. I dunno.) Paul --Paul M. Foster --PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php