Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6579199
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:52:07+00:00 2026-05-25T15:52:07+00:00

good evening, how to set custom error OR prevent page damage when display_errors=0 AND

  • 0

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" /></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 ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T15:52:07+00:00Added an answer on May 25, 2026 at 3:52 pm

    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;
        /////////////////////////////////////////////
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Good evening :-)! I have this code to use Drag & Drop method for
Good evening, I am developing a set of Java classes so that a container
Good evening, In my app that I'm currently developing, I have a class that
Good evening I have a JTable that I built with a TableModel how to
Good evening, I have one table, with a timestamp column, then I have a
Good evening/morning/after/noon. I have an ASP.net 3.5 website and I am using vb.net in
Good evening fellow stackers, I have a mutex and threaded related question about //
Good evening, I'm having trouble with model binding and validation but I don't know
Good evening Stack Overflow, I've got a css issue tonight. I have a fixed-positioned
Good evening, I am tired and have been fighting with this for hours. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.