简体   繁体   中英

How to keep browser from timing out (long PHP script)

The PHP script takes 25 seconds to complete (for 1 person) and probably longer with high traffic. I don't want the browser to time out (PHP won't - I set_time_limit(0); ).

Can someone provide sample code on how to make sure the browser doesn't time out (or else explain in detail what to do)? What's the best method? Ajax?

Thanks.

EDIT:

Brian Graham says, "Once javascript starts an ajax request, it runs until it finishes, quits or errors. As far as I know, it should even do this if the user leaves the page."

So, could I simply call the long PHP script via Ajax, and then it would run for however long is needed without ever timing out (even if nothing was returned by the script)?

That really depends on what your script is doing and what it's sending back, but if you can use AJAX that would be the way to go. Not only would it let you run the request for a long period of time, but wouldn't tie up the browser leaving the user free to continue using the current page until the request is done.

The best way to implement something like this that I've found is to set ignore_user_abort and turn off the time limit. This way you can let the script run in the background. This way the script can continue to run while the user browses your site. They don't even have to stay on the same page. So you fire off the request with AJAX to start it up. Have a loop in that script that updates the progress to the user's session.

ini_set('session.use_cookies', 0); //don't spam the user with cookies for each session_start()
while( ... ) {
    session_start();
    //update status to the user's session
    session_write_close();
    //sleep or whatever
}

Obviously this doesn't have to be a loop. You can simply close the session before you start processing information, then open it, set a status, and close it again. Only one script can access a user's session at a time, so you need to make sure this process isn't tying that up.

Then all you need to do is make a script that runs on the pages of your site that lets the user know when whatever it is you're doing is done and/or provides them the file/output/etc.

This frees up the user to continue browsing your site while your script is running and doesn't force them to sit on a single page staring at a loading indicator.

You'll need to initiate the main process with an AJAX request and then continually poll the server to check the progress. There's no way to tell a browser to not time out. (And understandably so. If my browser hung for 5 minutes I would be quite confused.)

I suggest assigning each run of the script a unique id, and then passing that back in the AJAX request. Then, in future status polls, just pass that ID, and the polling script can return the status. How you handle the status stuff will depend on what exactly is going on.

If you don't want the browser to timeout, then here are the only solutions I can possibly think for for you.... Just know this: I set php to time out after 10 seconds. If it takes longer than that to process - ever - then the script is not something that is suitable for users visiting my site.

1) Optimize your code - Find out what part of your code is taking too long, and determine what you can do to make it faster.

2) Use a push/pull solution. Push a request to the server and tell it to execute an application. The application is thrown into a queue to be run, and the server returns the process ID. A daemon on the server checks the queue and executes the script from cli rather than the browser.

3) create separate routines that require separate ajax-style method calls. Call routine 1 to start a piece of the script. Call routine 2 to make another ajax request and start the second after the first has returned it is finished. etc.

4) honestly - i'd just find another solution. obviously if your script is taking that long to execute, something is terribly wrong...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM