Jump to content

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/206084-force-headerlocation-redirect/
Share on other sites

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.

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?..

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.