strange behaviour max-execution-time php.ini

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
Hi NG,

I have my own apache server 2.0.54 running with php 4.3.10 on debian

I got a little logical problem here about http requests.

I have written a small php script which waits for 360 seconds. Every
second it appends the seconds to a file.

I expected a timeout after 300 seconds, because apache is configured
like that. But until now it doesn't matter. I can wait for more than 5
minutes and the script finishes i see the result page.
On the other hand I expected a timeout after 30 seconds, because
max-execution-time in php.ini is set to 30.

One apache group user said, that maybe the max-execution-time of
php.ini maybe only applies to CPU time (non-sleeping).?

Anotherone said, that I am not using the right php.ini. But in
phpinfo(); it's:

Configuration File (php.ini) Path: /etc/php4/apache2/php.ini

And that's the php.ini i am working with.

How I can prevent scripts of running endless??? Which parts are
responsible? Does my script stop on your installations after the time
of max-execution-time?

Here the code:


        // All print functions do not show up on the screen until it is
        print 'Now we wait<br>';

        for($i = 1; $i <= 360; ++$i) {
                print "$i<br>";
                $handle = fopen('test.dat', "a");
                fwrite($handle, $i . "\n");

        print '<br><br>End...<br>';



Re: strange behaviour max-execution-time php.ini

hakim wrote:

Quoted text here. Click to load it

Hi Hakim.

I saw this question (in another incarnation) a few times before here.
That probably means you are not very lucky getting answers.

(I have a few wild suggestions for you to test, but if that doesn't help  
you, you could submit a bugreport to Zend, with a code example.)

Quoted text here. Click to load it

this is wat has to say about that:
[source: ]
max_execution_time  integer

    This sets the maximum time in seconds a script is allowed to run before  
it is terminated by the parser. This helps prevent poorly written scripts  
from tying up the server. The default setting is 30.

    The maximum execution time is not affected by system calls, stream  
operations etc. Please see the set_time_limit() function for more details.

    You can not change this setting with ini_set() when running in safe  
mode. The only workaround is to turn off safe mode or by changing the time  
limit in the php.ini.

    Your webserver can have other timeouts. E.g. Apache has Timeout  
directive, IIS has CGI timeout function, both default to 300 seconds. See  
the webserver documentation for meaning of it.  

and set_time_limit:

void set_time_limit ( int seconds )

Set the number of seconds a script is allowed to run. If this is reached,  
the script returns a fatal error. The default limit is 30 seconds or, if it  
exists, the max_execution_time value defined in the php.ini. If seconds is  
set to zero, no time limit is imposed.

When called, set_time_limit() restarts the timeout counter from zero. In  
other words, if the timeout is the default 30 seconds, and 25 seconds into  
script execution a call such as set_time_limit(20) is made, the script will  
run for a total of 45 seconds before timing out.


set_time_limit() has no effect when PHP is running in safe mode. There is no  
workaround other than turning off safe mode or changing the time limit in  
the php.ini.

    Note: The set_time_limit() function and the configuration directive  
max_execution_time only affect the execution time of the script itself. Any  
time spent on activity that happens outside the execution of the script  
such as system calls using system(), stream operations, database queries,  
etc. is not included when determining the maximum time that the script has  
been running.  

The note is maybe of special interest to you because this might be excactly  
what the other user (in apachegroup) told you.
sleep() could just not count as time used up, but I don't know this for  

To test this I would suggest you replace you sleep(1) call by something that  
really uses up cpu time.
(eg: Some stupid loop, or try to calculate the first 1000 primenumbers, or  

What happens now?

Quoted text here. Click to load it

Well, that is simple to test.
Just change something in php.ini and check if it affects your php-script.
You can simple get the php,ini settings by using:

replace "somedirectivehere" with something you find here for example:

Quoted text here. Click to load it

Of course you can also program your own max_executiontime if this directive  
keeps giving you troubles.
Just remember the time when you script starts, and in your for/next or  
while-loop, check for time passed.
You can use microtime() for this purpose if you want to be excact.

Quoted text here. Click to load it

Looks fine to me...

Quoted text here. Click to load it

Good luck.
Please repost your results here.

Erwin Moller

Site Timeline