dqwh1219 2015-06-08 11:00
浏览 32
已采纳

在共享主机的生产中可以看到PHP错误

I am getting the below error in one of the file in Production, where the function is defined twice. I tried to recreate the issue, getting in a different file.

Fatal error: Cannot redeclare foo() (previously declared in /home/content/45/8989001/html/test/test.php:5) in /home/content/45/8989001/html/test/test.php on line 10

To suppress this error, its advised to make an entry to php.ini file, but I don't have access to it as its shared hosting.

Alternatively, its suggested to make an entry to existing php file inside the <?php ?> tags. I did the below change.

error_reporting(0); // This is not working, still error is displayed
error_reporting(E_NONE); // This is not working, still error is displayed
ini_set('display_errors', 0); // This is not working, still error is displayed

My complete code,

<?php
function foo() {
    return "foo";
}
function foo() {
    return "foo";
}

// error_reporting(0); // This is not working, still error is displayed
// error_reporting(E_NONE); // This is not working, still error is displayed
ini_set ( 'display_errors', 0 ); // This is not working, still error is displayed

echo "hello";

?>

Problem: How to suppress this error in Production, instead log to some file. or at-least suppress the error?

Note: The error is fixed in prod, but how to suppress it to avoid user seeing error in some other file next time

Update1:

After the below change too, the error is the same.

<?php
ini_set ( 'display_errors', 0 );

function foo() {
    return "foo";
}
function foo() {
    return "foo";
}

// error_reporting(0); // This is not working, still error is displayed
// error_reporting(E_NONE); // This is not working, still error is displayed
// ini_set ( 'display_errors', 0 ); // This is not working, still error is displayed

echo "hello";

?>

Update2:

<?php
register_shutdown_function( "fatal_handler" );
function fatal_handler() {
    echo "inside fatal_handler";
    $errfile = "test";
    $errstr  = "shutdown this test";
    $errno   = E_CORE_ERROR;
    $errline = 0;

    $error = error_get_last();

    echo $error;

    if( $error !== NULL) {
        $errno   = $error["type"];
        $errfile = $error["file"];
        $errline = $error["line"];
        $errstr  = $error["message"];

        $newline = "
";
        define ( 'senderName', 'Error' );
        define ( 'senderEmail', 'admin@abc.com' );
        $headers = "From: " . senderName . " <" . senderEmail . "
";

        $subject_admin = 'Error-Fix it';
        $body_admin = "Dear Admin, $newline$newline An error occured  $newline" . "error number : " . $errno . $newline . " error file : $errfile" . $newline . "error line :" . $errline . $newline . "error string : $errstr" . $newline;
        $body_footer = " **** This is an an auto-generated mail. kindly do not reply to this mail. Thank You. ****" . $newline . $newline;
        $body_admin = $body_admin . $newline . $newline . $newline . $body_footer;
        $to_admin1 = 'mymail@gmail.com';
        mail ( $to_admin1, $subject_admin, $body_admin, $headers );
        //error_mail(format_error( $errno, $errstr, $errfile, $errline));
    }
}

function foo() {
    return "foo";
}
function foo() {
    return "foo";
}

// error_reporting(0); // This is not working, still error is displayed
// error_reporting(E_NONE); // This is not working, still error is displayed
// ini_set ( 'display_errors', 0 ); // This is not working, still error is displayed

echo "hello";
?>

update 3

Still getting same error

<?php
register_shutdown_function ( "fatal_handler" );
function fatal_handler() {
    echo 'YAY IT WORKED';
}
function foo() {
    return "foo";
}
function foo() {
    return "foo";
}

// error_reporting(0); // This is not working, still error is displayed
// error_reporting(E_NONE); // This is not working, still error is displayed
// ini_set ( 'display_errors', 0 ); // This is not working, still error is displayed

echo "hello";
?>

Update4

<?php 
//error_reporting(0);

function fatalErrorHandler() {
    echo 'YAY IT WORKED';
}

# Registering shutdown function
register_shutdown_function('fatalErrorHandler');

// let force a Fatal error -- function does not exist
functiontest();

echo "hello";

?>
<!-- 
output:

Fatal error: Call to undefined function functiontest() in ...test2.php on line 12
YAY IT WORKED -->

<?php 
error_reporting(0); 

function fatalErrorHandler() {
    echo 'YAY IT WORKED';
}

# Registering shutdown function
register_shutdown_function('fatalErrorHandler');

// let force a Fatal error -- function does not exist
functiontest();

echo "hello";

?>

<!-- output:

YAY IT WORKED -->

Final update:

This resolved my intermediate question register_shutdown_function is not getting called

  • 写回答

1条回答 默认 最新

  • dongzhang6021 2015-06-08 11:11
    关注

    If I were you I would call my host and ask them to make the changes to my phpini. They may have special things setup.

    Now you should never just hide errors. You send yourself an email and fix right away. The error you are trying to surpress is fatal and the script will no longer run, so hiding it only results in a blank page, the user stkill cannot continue.

    It is a fatal error and you cannot recover from it. The way to hide fatal error properly in my opinion is to create your own error handler function.

    And you would use something like this to catch the fatal errors. http://php.net/manual/en/function.register-shutdown-function.php

    The function needs to be on every page in order to catth the error. I have a error.php that I require in my db class, which is on every page.

    //This function gets called every time your script shutsdown
    register_shutdown_function( "fatal_handler" );
    
    function fatal_handler() {
      $errfile = "unknown file";
      $errstr  = "shutdown";
      $errno   = E_CORE_ERROR;
      $errline = 0;
    
      $error = error_get_last();
    
      if( $error !== NULL) {
        $errno   = $error["type"];
        $errfile = $error["file"];
        $errline = $error["line"];
        $errstr  = $error["message"];
    
        //send error email
        error_mail(format_error( $errno, $errstr, $errfile, $errline));
      }
    }
    

    UPDATE

    **OP said the function is not being called. **

    The above should work.

    Here is basically what I use in production, it should tottally work.

    ON_SCREEN_ERRORS and SEND_ERROR_EMAIL are my own config constants for development.

    //Set error handler function (See function below)
    set_error_handler("StandardErrorHandler");
    
    /**
     * Will take an error string and if ON_SCREEN_SQL_ERRORS  is set to on, it will display the error on the screen with the SQL in a readable format and
     * if SEND_SQL_ERROR_EMAILS is set to on, it will dendthe error email with the SQL in a readable format.
     * 
     *  PHP will pass these parameters automatically on error.
     *
     * @param string $errno The error type code.
     * @param string $errstr The error string.
     * @param string $errfile The file that the error occured in.
     * @param string $errline The line number that the error occured.
     */
    
    function StandardErrorHandler($errno, $errstr, $errfile, $errline) {
        $Err = $errstr.'<br />'.GetErrorType($errno).'on line '.$errline.' in '.$errfile.'<br />';
        if (ON_SCREEN_ERRORS===TRUE)
        {
            err($Err);
        }
        if ($errno =='256' and SEND_ERROR_EMAILS === TRUE)
        {
            $Path = "http://". $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
            // Custom error function           
            gfErrEmail($Err, $Path, 'SQL Error');
        }
    }
    
    
    //Set the Fatal Error Handler function (See function below)
    register_shutdown_function("FatalErrorHandler");
    
    /**
     * This function gets called on script shutdown, it will check if the last error is a fatal error. You cannot catch fatal errors,
     * but using this function we will be notified about it and be able to fix it.
     * If error is fatal, and if ON_SCREEN_FATAL_ERRORS is set to ON, this function will display the fatal error on the screen.
     * If error is fatal, and if SEND_FATAL_ERROR_EMAILS is set to ON, this function will send error email.
     *
     */
    
    function FatalErrorHandler() {
    
        $error = error_get_last();
        if($error !== NULL) {
             //check if error is of fatal, compliler or other non recoverable error types
            if ($error["type"] =='1' || $error["type"] =='4' || $error["type"] =='16' || $error["type"] =='64' || $error["type"] =='4096')
            {
                $errno  = GetErrorType($error["type"]);
                $errfile = $error["file"];
                $errline = $error["line"];
                $errstr  = $error["message"];
                $Err = '<strong>'.$errno.'<br/></strong>'.$errstr.'<br />'.$errno.' on line '.$errline.' in '.$errfile.'<br />';
                if (ON_SCREEN_ERRORS===TRUE)
                {
                    err($Err);
                }
                if (SEND_ERROR_EMAILS === TRUE)
                {
                    $Path = 'http://'. $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];                
                    //Custom function
                    gfErrEmail($Err, $Path, $errno);
                }
            }
        }
    }
    
    
    
    /**
     * This function receives the error code and returns the specified string. 
     * The return strings are what the error message will display.
     *
     * @return string The error title
     */
    function GetErrorType($Type)
    {
        switch($Type) 
        { 
            case 1:
                // 'E_ERROR'; 
                return 'Fatal Error ';
            case 2:
                // 'E_WARNING'; 
                return 'Warning ';
            case 4:
                // 'E_PARSE'; 
                return 'Compile Time Parse Error ';
            case 8:
                // 'E_NOTICE'; 
                return 'Notice ';
            case 16:
                // 'E_CORE_ERROR'; 
                return 'Fatal Start up Error ';
            case 32:
                // 'E_CORE_WARNING'; 
                return 'Start Up Warning ';
            case 64:
                //'E_COMPILE_ERROR'; 
                return 'Fatal Compile Time Error ';
            case 128:
                // 'E_COMPILE_WARNING'; 
                return 'Compile Time Warning ';
            case 256 :
                // 'E_USER_ERROR' - USED FOR SQL ERRORS - DO NOT USE THIS ERROR CODE to TRIGGER_ERROR()
                return 'SQL Error ';
            case 512:  
                // 'E_USER_WARNING'; 
                return  'Warning - Thrown using trigger_error() ';
            case 1024:  
                // 'E_USER_NOTICE'; 
                return  'Notice - Thrown using trigger_error() ';
            case 2048: 
                // 'E_STRICT'; 
                return 'Strict Error (PHP suggest changes to your code which will ensure the best interoperability and forward compatibility of your code.) ';
            case 4096: 
                // 'E_RECOVERABLE_ERROR'; 
                return 'Catchable Fatal Error (This error can be caught, use a Try Catch) ';
            case 8192: 
                // 'E_DEPRECATED'; 
                return 'Warns you of depreciated code that will not work in future versions of PHP. ';
            case 16384: 
                // 'E_USER_DEPRECATED'; 
                return  'Programmer Triggered Error - Thrown using trigger_error() ';
        } 
        return "Error Type Undefined "; 
    } 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥15 复杂网络,变滞后传递熵,FDA
  • ¥20 csv格式数据集预处理及模型选择
  • ¥15 部分网页页面无法显示!