在共享主机

时间:2015-06-08 11:00:13

标签: php

我在Production中的一个文件中收到以下错误,其中函数定义了两次。我尝试重新创建问题,进入另一个文件。

  

致命错误:无法重新声明foo()(之前在声明中声明)   /home/content/45/8989001/html/test/test.php:5)in   第10行/home/content/45/8989001/html/test/test.php

要取消此错误,建议您输入php.ini文件,但我无法将其作为共享托管进行访问。

或者,它建议在<?php ?>标签内输入现有的php文件。我做了以下更改。

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

我的完整代码,

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

?>

问题:如何在生产中抑制此错误,而是登录到某个文件。或者至少抑制错误?

注意:错误在prod中已修复,但是如何抑制它以避免用户下次在其他文件中看到错误

UPDATE1:

以下更改后,错误也是一样。

<?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 = "\r\n";
        define ( 'senderName', 'Error' );
        define ( 'senderEmail', 'admin@abc.com' );
        $headers = "From: " . senderName . " <" . senderEmail . "\r\n";

        $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";
?>

更新3

仍然得到同样的错误

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

最终更新:

这解决了我的中间问题 register_shutdown_function is not getting called

1 个答案:

答案 0 :(得分:2)

如果我是你,我会打电话给我的主人并要求他们对我的phpini进行更改。他们可能会设置特殊的东西。

现在你永远不应该只是隐藏错误。您自己发送电子邮件并立即修复。您尝试压缩的错误是致命的,并且脚本将不再运行,因此隐藏它只会导致空白页面,用户stkill无法继续。

这是一个致命的错误,你无法从中恢复。在我看来,正确隐藏致命错误的方法是创建自己的错误处理函数。

你会用这样的东西来捕捉致命的错误。 http://php.net/manual/en/function.register-shutdown-function.php

该功能需要在每个页面上才能发生错误。我在我的db类中需要一个error.php,它位于每个页面上。

//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));
  }
}

更新

** OP表示该函数未被调用。 **

上述情况应该有效。

这基本上是我在制作中使用的,它应该是有效的。

ON_SCREEN_ERRORS和SEND_ERROR_EMAIL是我自己的开发配置常量。

//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 "; 
} 
相关问题