views:

63

answers:

4

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;
}
?>
A: 

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.

ThiefMaster
No way using error handler? I mean it shows that it's a Fatal Error.. :-\
Bibhas
Fatal Errors cannot be handled by custom error handlers in PHP.
ThiefMaster
A: 

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

Polygraf
A: 

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

Piotr Pankowski
This interrupts running functions, e.g. database queries.
ThiefMaster
A: 

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

Bibhas