Post Output Processing

Hi. I am looking for a method to do maintenance tasks such as deleting temporary files and stale sessions from the database after outputting content. The current method I use is to generate a random number in my logic class destructor (after content has been output) and if that number matches a number I have, the maintenance functions run (approximately 1 out of every 250 pageviews).

The problem is that if these tasks take a while, the server does not close the connection after all output has been received by the browser making it appear that the browser is waiting for more from the server.

It seems PHP is lacking in the ability to continue executing code after all content has been output. Researching the issue, it looks like the common solution is to buffer the output, get the length of the output, then output headers in code to close the connection with the content length. (Example) I wonder if there is a better way.

I would prefer not to have to rewrite my script to close the connection and measure the content length in my code. So, if there is no other way to do this, then I guess I’ll have to set up a cron job to run a PHP script once per day to delete temporary files and stale database sessions. (I’ll have to learn how to do this, too.)

Does anyone have suggestions?

You have a cuple of options open to you.

Run a script which is triggered by a timer - on linux its called cron and on windows its the scheduler.

If you don’t have access to that then you can use a remote cron service - which basically just calls your .php script at a chosen time/interval. You just create the script to do the cleanup and then exit. If its a long running script then the connection might be closed at the other side so use ignore_user_abort() to ensure it continues to run.

In older versions of PHP, [fphp]register_shutdown_function[/fphp] would have been interesting for you, as it used to allow execution of functions after all output had been sent to the browser and the connection had been closed. It doesn’t work that way any more though.

To be honest, if you can run a cron on your server, do so. Writing PHP for the command line might seem a little daunting at first, but after about 20 minutes you’ll wonder how you ever managed without :slight_smile:

Used to? - I’m still using php4 on a wamp setup… I’ve also got code running on a site running php5 and for me it still works.

What am I missing?

Thanks a lot for the tip. :slight_smile: I read through the comments for register_shutdown_function() and there is mention of a change in the function enabling shutdown functions to send output to the browser. In such case, the connection isn’t closed and would result in the same thing I experience now: the browser appearing to wait for more content.

I’m sure it can be tested using a sleep() call and then seeing if the browser appears to wait for more content.

Thanks! :slight_smile:

Edit: I tested it using a sleep(20) call in the function I called in register_shutdown_function() and PHP does not send content until after the sleep is over. So, register_shutdown_function() does not close the connection and then continue executing PHP code. It waits to output content and close the connection until everything in the shutdown function completes.

You’re still missing the point - You’re asking how to do maintenance tasks without hanging the browser.

You need to use a script setup to run on a timer - not a call via http from a user. Additionally if you’re using sessions and using register_shutdown_function you will lock the users session until you are finished with your maintenance task. You can get around this by using session_write_close().