Archive

Posts Tagged ‘Linux’

PHP – Redirect and continue (without abort)

June 28th, 2009 Andrew 2 comments

The Problem

Sometimes, in PHP, we have to do things that take a long time (like converting user-submitted videos, say), but there’s a problem with that: the user has to wait until we’re done with whatever we’re doing.  The worst part is, it looks like their request was dropped because the browser just sits there “loading” forever and shows no sign of progress until the page is finally done loading or – *shudder* – times out.

Solutions

There are a few strategies to solve this problem:

  1. You could have your PHP script create a cron job on the server.  This approach is overly complex in my opinion and has some problems: You have to be running your PHP code on a Linux server, and you have to have access to cron – not really an option for most people who don’t host their own server.
  2. You could write a special PHP script that gets called via AJAX after the page is loaded and returns the results from the operation.  That works just fine and is a great option for someone comfortable with AJAX.  I’ve actually used this strategy myself, but it’s still too complicated for my taste.
  3. My weapon of choice: The PHP redirect and continue.

Read more…

rss
report
e-mail