PHP错误在共享主机的生产环境中可见
我在生产环境中的一个文件中得到以下错误,其中函数定义了两次。我试着重新创建这个问题,得到了一个不同的文件 致命错误:无法重新声明foo()(以前在中声明 /home/content/45/8989001/html/test/test.php:5) /第10行的home/content/45/8989001/html/test/test.php 为了抑制此错误,建议输入PHP错误在共享主机的生产环境中可见,php,Php,我在生产环境中的一个文件中得到以下错误,其中函数定义了两次。我试着重新创建这个问题,得到了一个不同的文件 致命错误:无法重新声明foo()(以前在中声明 /home/content/45/8989001/html/test/test.php:5) /第10行的home/content/45/8989001/html/test/test.php 为了抑制此错误,建议输入php.ini文件,但我无法作为共享主机访问它 或者,建议在标记中输入现有的php文件。我做了下面的改变 error_report
php.ini
文件,但我无法作为共享主机访问它
或者,建议在
标记中输入现有的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中已修复,但如何抑制它以避免用户下次在其他文件中看到错误
更新1:
<?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";
?>
在下面的更改之后,错误是相同的
<?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";
?>
更新2:
<?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";
?>
更新4
<?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 -->
最后更新:
这解决了我的中间问题
如果我是你,我会打电话给我的主人,让他们更改我的phpini。他们可能有特殊的设置 现在你不应该只是隐藏错误。你给自己发一封电子邮件,然后马上修复。您试图阻止的错误是致命的,脚本将不再运行,因此隐藏它只会导致空白页,用户stkill无法继续 这是一个致命错误,您无法从中恢复。在我看来,正确隐藏致命错误的方法是创建自己的错误处理函数 你可以用这样的方法来捕捉致命的错误。 为了消除错误,该函数需要出现在每一页上。我在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说这个函数没有被调用**
上述方法应该有效。
这里基本上是我在生产中使用的,它应该可以正常工作
屏幕错误和发送错误电子邮件是我自己的开发配置常量
//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 ";
}
//设置错误处理函数(参见下面的函数)
设置错误处理程序(“StandardErrorHandler”);
/**
*将获取一个错误字符串,如果ON_SCREEN_SQL_ERRORS设置为ON,它将在屏幕上以可读的格式显示错误,并且
*如果SEND_SQL_ERROR_EMAILS(发送SQL错误电子邮件)设置为on(打开),它将以可读的格式发送带有SQL的错误电子邮件。
*
*PHP将在出错时自动传递这些参数。
*
*@param string$errno错误类型代码。
*@param string$errstr错误字符串。
*@param string$errfile发生错误的文件。
*@param string$errline发生错误的行号。
*/
函数StandardErrorHandler($errno、$errstr、$errfile、$errline){
$Err=$errstr.'
'。GetErrorType($errno)。'on line'.$errline.'in'.$errfile.
;
如果(屏幕上的错误===真)
{
err($err);
}
如果($errno='256'并发送\u错误\u电子邮件===TRUE)
{
$Path=“http://”$\u服务器['http\u主机]。$\u服务器['REQUEST\u URI'];
//自定义错误函数
gfErrEmail($Err,$Path,'SQL Error');
}
}
//设置致命错误处理程序函数(请参阅下面的函数)
寄存器关闭功能(“FatalErrorHandler”);
/**
*在脚本关闭时调用此函数,它将检查最后一个错误是否为致命错误。您无法捕捉致命错误,
*但使用此功能,我们将收到通知并能够修复它。
*如果错误是致命的,并且如果ON_SCREEN_fatal_ERRORS设置为ON,则此功能将在屏幕上显示致命错误。
*如果错误是致命的,并且如果SEND_fatal_error_EMAILS设置为ON,则此功能将发送错误电子邮件。
*
*/
函数FatalErrorHandler(){
$error=error_get_last();
如果($error!==NULL){
//检查错误是否为致命错误、compliler错误或其他不可恢复的错误类型
如果($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=''.$errno.
.$errstr.
.$errno.'on line'.$errline.'in'.$errfile.
.';
如果(屏幕上的错误===真)
{
err($err);
}
如果(发送错误电子邮件===真)
{
$Path='http://'.$\u服务器['http\u主机].$\u服务器['REQUEST\u URI'];
//自定义函数
gfErrEmail($Err,$Path,$errno);
}
}
}
}
/**
*此函数接收错误代码并返回指定的字符串。
*返回字符串是错误消息将显示的内容。
*
*@returnstring返回错误标题
*/
函数GetErrorType($Type)
{
交换机($类型)
{
案例1:
//“E_错误”;
返回“致命错误”;
案例2:
//“E_警告”;
返回“警告”;
案例4:
//“E_PARSE”;
返回“编译时分析错误”;
案例8:
//“E_通知”;
返回“通知”;
案例16:
//“E_核心错误”;
返回“致命启动错误”;
案例32:
//“E_核心警告”;
返回“启动警告”;
案例64:
//“E_编译_错误”;
返回“严重编译时错误”;
案例128:
//“E_编译_警告”;
返回“编译时警告”;
案例256:
//“E_用户_错误”-用于SQL