2

I was expecting some error like 500 or timeout page with the following code:

<?php
      ini_set('max_execution_time',5);
      set_time_limit(5);
      echo 'start';
      sleep(10);
      echo '<br/>hi';

However, I get something like this:

start
hi

Did I do something incorrect?

All I want is see the script stoped when timesout in the 5thd second, so the second echo should not be executed(I know this is quite a weird requirement)

Could anyone shred a light, thanks.

PS: seems the sleep() part is quite a distraction, how about I change the code like this:

<?php
      ini_set('max_execution_time',5);
      set_time_limit(5);
      echo 'start';
      for($i=1;$i<100000000;$i++){
          if($i%100==2) echo $i;
          else echo '--';
      }
      echo '<br/>hi';
6
  • I'm not going to make this an answer, because you're probably running the latest version of PHP. But if you're running an older version with safe_mode turned on (before that feature was removed) then set_time_limit and ini_set('max_execution_time') have no effect. Commented Aug 9, 2012 at 4:16
  • hi in my phpinfo, I can see max_execution_time but no set_time_limit Commented Aug 9, 2012 at 4:27
  • @zhaopeng do you have any sleep calls in your "work" code? How do you know that it's taking longer than the max_execution_time? Commented Aug 9, 2012 at 4:29
  • That's normal. Max_execution_time is a configuration directive that can be set in your php.ini file (that's how it has to be set when safe mode is on) while set_time_limit() is a function called within a script that just affects the rest of that script. Commented Aug 9, 2012 at 4:33
  • @J, I don't have sleep in my work code. you can actually change the sleep part, to a loop which runs, say 1000000000 times, to echo something, which of course will be more than the max_execution_time I set(1 second) Commented Aug 9, 2012 at 6:00

1 Answer 1

6

According to this comment in php.net you must be using unix... "Please note that, under Linux, sleeping time is ignored, but under Windows, it counts as execution time." (Sleep is not taken into consideration as part of the execution time in Unix/Linux.)

Additionally, to make it timeout simply loop forever

<?php
while (true) {
    // i'll error out after max_execution_time
}

To see how long that is, you can either find out the execution time using microtime or inquire about the max_execution_time variable you just set.

$max_time = ini_get("max_execution_time");
echo $max_time
?>

UPDATE With your updated code, the output is as expected.

Fatal error: Maximum execution time of 5 seconds exceeded in newfile.php on line 7
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks J. But in my example, sleep is merely something I try to make the script running long. It could be some other long-running code instead. By the way, yes, it is running under Linux tho.
So this is the correct answer then. In linux, time spent sleep()ing is ignored.
To see how long that is, you can either find out the execution time using microtime or inquire about the max_execution_time variable you just set using ini_get('max_execution_time');
I never knew this! Not that I've ever needed to sleep() until execution timed out. A quick test on my server seems to confirm (I was dubious since the comment was posted in 2007). Anyone have any details about why?
@J, with your way "while(true)", I get a dead-loop and this time I use ini_set to make max_execution = 1.... so weird..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.