piramidon Posted June 28, 2010 Share Posted June 28, 2010 Hi, I have a php page where a user has to enter some information, which is then processed. As the processing takes a while, I want to take the user to another page while it's running. So, I have a header('Location: other_page.php'), after which I insert the rest of the code. However, the redirect only takes place after all the other code is executed. So, my question is: Is there a way to force a redirect and let the code work "in the background"? If not, do you have any suggestions how I can call a function that needs a few minutes to complete without the user being locked to a page? Thanks! Quote Link to comment Share on other sites More sharing options...
Psycho Posted June 28, 2010 Share Posted June 28, 2010 There are ways to do this. For example you could store the form data in a queue table and have a cron job that runs every x minutes to process any submissions. The problem with this approach is that the submission/processing is no longer part of the user's session since the processing is done in a separate process. How long is the script taking to run? You could display a page with a message such as "Your data is being processed" with an indeterminate progress indicator. But, more importantly, have you investigated the code to see that it is running as efficiently as it can? For example, do you have any database queries running in a loop? Those are extremely inefficient and can almost always be done with a single query. Quote Link to comment Share on other sites More sharing options...
piramidon Posted June 28, 2010 Author Share Posted June 28, 2010 Hi, thanks for the quick answer. I do have database requests, and some of them do indeed run in loops. But the largest part of the runtime is from having to retrieve pages from the web (with file_get_contents) and checking them against files already stored. This is done with similar_text, which is quite slow; if you have any idea how to do it faster, I'd be very grateful (as far as I understand, levenshtein is faster but only works on very short strings). The whole processing takes about 2 to 5 minutes. Isn't there some equivalend of flush for headers?.. Quote Link to comment Share on other sites More sharing options...
Psycho Posted June 28, 2010 Share Posted June 28, 2010 Another thought is to use AJAX and process the requests asynchronously. However, once the user navigates to another page - either on their own or through header() the javascript request cannot "report back" to the user once the process is complete. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.