Question

I've seen a lot of questions regarding this error in PHP. But everywhere the solution is given as to set the time limit.

But what I want to do is, to detect the error and if it occurs then switch to another course of action. I mean, if my requests exceed my maximum time set, then it should not give an error message and stop but continue executing some other block of codes. How may I achieve that?

=======

I just found this code is a site, can it help?

<?php

//open error reporting, the error message is required in this script
error_reporting(1);

//for testing, set maximum execution to 1 second
set_time_limit(1);

//turn on output buffering and set callback function
ob_start('callback');

//------------------------------------------------------------------------------

//main program(dead loop)
while (true);

//script completed(impossible)
echo 'Completed!';

//------------------------------------------------------------------------------

/**
 * output callback, execute when end of the request
 *
 * @param string $buffer
 * @return string
 */
function callback($buffer) {
    if (false !== (stripos($buffer,'<b>Fatal error</b>'))) {
        if (false !== (stripos($buffer,'Maximum execution'))) {
            $buffer = 'ycTIN.com , Server Busy';
        } else {
            $buffer = 'ycTIN.com , Server Internal Error';
        }
    }
    return $buffer;
}
?>
Was it helpful?

Solution 4

I guess no way then. I just set the time limit to infinite. That way it should end sometime.. :)

OTHER TIPS

This is not easily possible. You could save the current time at the beginning of the script and then call a function which checks the time difference every few lines. This is not very efficient though and it will clutter your code.

It can't be done because exceeding your max execution time will result in a fatal error in php

pcntl_alarm may help if you set it to value smaller than max execution time.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top