开发者

php - how to prevent page damage when display_errors=0 and the page have errors?

开发者 https://www.devze.com 2023-04-05 05:17 出处:网络
good evening, how to set custom error OR prevent page damage when display_errors=0 AND the page have errors AND i do not know what \'condition, code, etc...\' will cause these errors ?

good evening,

how to set custom error OR prevent page damage when display_errors=0 AND the page have errors AND i do not know what 'condition, code, etc...' will cause these errors ?

for example, in this code :

<?php
    /////////////////////////////////////////////
        ini_set("display_errors" , "0");
    /////////////////////////////////////////////
        $html  = '
            <table>
                <tr>
                    <td><img src="img0.gif" /></开发者_C百科td>
                    <td><img src="img1.gif" /></td>
                    <td><img src="img2.gif" /></td>
                </tr>
            </table>
        ';
    /////////////////////////////////////////////
        $dom   = new domDocument();
        $dom  -> loadHTML($html);
        $xpath = new domXPath($dom);
    /////////////////////////////////////////////
        $query   = $xpath->query('.//table/tr/td');
        $image   = $query->item(1000000000); // <<---- item number 
        $img_src = $image->getElementsByTagName('img')->item(0)->getAttribute('src'); 
        echo $img_src;
    /////////////////////////////////////////////
?>

in that code because there is no item(1000000000), so it will cause error ..

and bacause display_errors" , "0" all errors will be hidden and the page be damaged

So, how to prevent that damage ?

i know i can prevent it by adding simple condition like :

if( $image <1 ){ die('error msg'); } before $img_src var

but what about if i have a big code more thane 10000 line and i do not know exactly what code will cause error !!

can we prevent it by httaccess ? by checking page source if it empty redirect to error page ..

or can we prevent it by some functions in php itself ?

any ideas ?


You can use set_error_handler . To set a callback ion case of error and in there you can terminate the script from executing further.

Update
Just found out custom error handler does not work with E_ERROR and other error types. This is in PHP Documentation

The following error types cannot be handled with a user defined function: E_ERROR, E_PARSE, E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR, E_COMPILE_WARNING, and most of E_STRICT raised in the file where set_error_handler() is called.

So this is a workaround that I found on internet and I have tested it with your script and is working fine.
Add the following code to the start of your script:

// execute on shutdown
function shutdown_handle(){
    // get last error details if any
    $lerror = error_get_last();

    // if there is a Fatal (E_ERROR Type) las error then redirect to error page
    if($lerror && $lerror['type'] == E_ERROR)
        header('Location:error_page.php');
}
// register our shutdown handler
register_shutdown_function('shutdown_handle');

This registers a shutdown function which is executed on script termination from a fatal error, irrespective of whether display_errors is on or off.

And there we check if last error was a fatal one, if so we redirect to error_page.php.

Note: One thing to note is that the redirect is STILL working if display_errors is ON. I have tested it and it is working fine. This maybe because of output_buffering being enabled on my server though.

Update 2:
This is the complete PHP file with Your code with shutdown handler added. This works just fine and redirects to error_page.php. Change this to the page you want to redirect to.:

<?php
    ////////////////////////////////////////////////
    // Shutdown handler to catch E_ERROR on shutdown 
    // even with display_errors OFF

    // function which will execute on shutdown
    function shutdown_handle(){
        // get last error details if any
        $lerror = error_get_last();

        // if there is a Fatal (E_ERROR Type) last error
        // then redirect to error page
        if($lerror && $lerror['type'] == E_ERROR)
            header('Location:error_page.php');
    }
    // register our shutdown handler
    register_shutdown_function('shutdown_handle');
    //////////////////////////////////////////////////

    /////////////////////////////////////////////
        ini_set("display_errors" , "1");
    /////////////////////////////////////////////
        $html  = '
            <table>
                <tr>
                    <td><img src="img0.gif" /></td>
                    <td><img src="img1.gif" /></td>
                    <td><img src="img2.gif" /></td>
                </tr>
            </table>
        ';
    /////////////////////////////////////////////
        $dom   = new domDocument();
        $dom  -> loadHTML($html);
        $xpath = new domXPath($dom);
    /////////////////////////////////////////////
        $query   = $xpath->query('.//table/tr/td');
        $image   = $query->item(1000000000); // <<---- item number 
        $img_src = $image->getElementsByTagName('img')->item(0)->getAttribute('src'); 
        echo $img_src;
    /////////////////////////////////////////////
?>


If you want to hide all errors from your users, enable error_reporting (so that the error still gets logged) and disable display_errors.

If you want all errors to be fatal (i.e. stop page processing) you can set a custom error handler which dies on every error.

0

精彩评论

暂无评论...
验证码 换一张
取 消