1

At first I want to point out, that this is not the same question as the others out there, because I don't want to increase the maximum execution time.

But to my problem: I have a long poll, so an AJAX-POST with timeout (higher than the maximum execution time of the PHP, so I don't have to worry about this value). I now recognized that if I leave my computer with opened browser (and running xampp), the POST won't be interrupted (like on reloading the page). When I get back to the computer (Windows, it went into sleep-mode), the script won't stop with my timer...:

$loop = array(
    "count"     => 0,
    "sleeptime" => 2,
    "maxec"     => 30,
    "start"     => time(),
);
$change = false;
while ($change) {
    $loop["count"]++;
    $looptime = ((time() - $loop["start"]) / $loop["count"]);
    if ($looptime * ($loop["count"] + 1) >= $loop["maxec"] - $looptime) {
        $response["s"] = 1; // $response is an array which will be printed at the end / JSON format
        $change = true;
    } else {
        sleep($loop["sleeptime"]);
    }
    // check my database
    // if there is an update, set $change = true and print the update
}

... but gives me the standard-error-message:

Fatal error:  Maximum execution time of 30 seconds exceeded in *file*.php on line 20

And my timer works at every* other situation... (* so far I'm able to say "every" ;) )

So I'm searching for a better way to print my response, e.g. {"response":1}, instead of the error-message. So I need a better working timer or a complete other solution.

I found this via google:
http://www.php.net/manual/en/function.register-shutdown-function.php
But I can't imagine how this could help me getting "before" the fatal error.

hffmr
  • 128
  • 9
  • 1
    exactly how would this work? To execute your shutdown code means running MORE code, which means you're running up the execution clock, but you've already exceeded the limit. In other words, you're trying to bypass the limit, or in human terms "lemme sleep 5 more minutes" when you're already late for work. – Marc B Jun 30 '14 at 18:46
  • I said "But I can't imagine how this could help me getting 'before' the fatal error." because I know that I can't do this. But I don't have any idea, why my timer doesn't work in this case. It would also help me, if you could say that there is no chance to get this on a real server... (I guess my circumstances aren't representative) – hffmr Jun 30 '14 at 18:51
  • I don't think there's anything in PHP that lets you get the "currently used execution time" counter. maybe I'm wrong, but in any case, it's a fatal error. you can't trap the error, and even if you COULD check that timer, there'd be nothing to stop it from hitting max while it's running your "oops, I'm almost at the limit" code anyways. – Marc B Jun 30 '14 at 18:52
  • And can I change this message to my text, so in a way to change only the 'format' of the error? – hffmr Jun 30 '14 at 18:54
  • short of hacking up the php source code, nope. there's no way to control those errors from "front-end" code. – Marc B Jun 30 '14 at 18:54
  • Ok, thanks for your fast answers! – hffmr Jun 30 '14 at 18:57
  • I think your answer is here: http://stackoverflow.com/questions/6861033/how-to-catch-the-fatal-error-maximum-execution-time-of-30-seconds-exceeded-in-p – Len_D Jun 30 '14 at 20:37
  • Thank you, too. It's the answer - I hope I won't get this on my servers and I'm going to provoke situations which can cause this error in my testphase so I will be able to handle these events. – hffmr Jul 01 '14 at 15:39

0 Answers0