1

I have a PHP script which contains many database queries, and copies several database tables, and as such, it takes quite a long time to complete. The problem I am getting, is that it is timing out. However, it appears to be completed, which is what is confusing.

The script is suppose to redirect to view once completed. However, even after extending the time limit to 5 minutes, it gives me the timing out error page. However, when I check the database, all of the tables have been copied completely, indicating that the script was completed.

Am I missing something easy here? Is there a general reason it would time out as opposed to redirecting to the view? I would post some of the code, but the entire script is approximately 1000 lines of code, so it seems a bit extensive to show here.

Also, I am using CodeIgniter.

Thanks in advance for your help!

jldavis76
  • 812
  • 2
  • 15
  • 42
  • timing out where in the script? not the timer isn't a hard-fast limit that kills the script the instant it's reached. if php is busy doing something in an external library (e.g. waiting for a mysql call to finish), it won't check the timer and abort the db call. it'll check the timer when the db call returns and control returns to php. – Marc B Feb 25 '16 at 19:59

1 Answers1

2

It's possible that the PHP script is not timing out, but the browser you're using has given up waiting for any result. If thats the case you'll need to handle the whole thing differently. For example, run the script in the background and report periodic updates via AJAX or something.

Think of it this way:

  1. Your browser asks your server for a web page and waits for the results.

  2. Your server runs your PHP script, which then asks MySQL to run a query, and waits for results.

  3. MySQL runs the query and returns a result to PHP.

  4. PHP does some more processing and returns a result to the browser.

At step 3, PHP may have timed out and is no longer there. MySQL didn't know that while it was working, it just did its job and then handed a result back to nothing.

At step 4, the browser may have timed out and dropped the connection. PHP wouldn't know that, so it did its job and then returned a result to nothing.

It's two separate timeouts in this example, but your query was completed either way.

Scott Saunders
  • 29,840
  • 14
  • 57
  • 64
  • In other words, your browser decided that your time was up, not PHP. – Scott Saunders Feb 25 '16 at 20:07
  • I imagine it probably is the browser that is timing out. But why would the tables be copied immediately after the browser timed out, regardless of the time? For example, if I set the time out limit to be 30 seconds, it times out after 30 seconds, but the tables are copied. If I set the time out limit to be 180 seconds, it times out after 3 minutes, but the tables are copied. – jldavis76 Feb 25 '16 at 20:10
  • Are they large tables that take longer than 30 seconds to copy? What database are you using? Perhaps the DBMS has received the copy command and continues copying even if PHP has stopped. – Scott Saunders Feb 25 '16 at 20:13
  • It's not so much that they are large tables, I don't think, but there are quite a few of them. I'm using mySQL. – jldavis76 Feb 25 '16 at 20:14
  • Yes, it seems to be working perfectly, other than the user getting a timed out error. – jldavis76 Feb 25 '16 at 20:21
  • I've added to my answer. – Scott Saunders Feb 25 '16 at 20:26
  • Thanks, Scott! So, your suggestion would be to run the script through an AJAX request, and provide updates to the browser? – jldavis76 Feb 25 '16 at 20:36
  • Yes. See this question: http://stackoverflow.com/questions/858883/run-php-task-asynchronously?rq=1 – Scott Saunders Feb 25 '16 at 20:47