Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 437
Hope4You Avatar asked Jun 14 '26 15:06

Hope4You


2 Answers

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.

like image 171
Brian Avatar answered Jun 16 '26 07:06

Brian


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.

like image 35
Corbin Avatar answered Jun 16 '26 07:06

Corbin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!