我曾经在php中渲染我的所有html,然后在完成后回显一个html字符串。当我的测试站点发生php错误(这是开发期间的错误,就像拼错的变量)我会回应错误并退出。很容易看到错误并快速修复它。当网站上线时,我会显示一般错误并通过电子邮件发送错误详细信息。这些实时错误不太常见,电子邮件也很完美。
目前我正在使用javascript / jquery渲染页面。现在,当发生PHP错误时,错误不会在页面上呈现。我可以通过电子邮件收到错误,但有时我甚至不知道发生了错误。控制台中没有错误,除页眉和页脚外,页面将为空白。
因此,为了显示php错误,我先做我所有的PHP代码。错误将按预期显示。在PHP中我回显了页眉和页脚html。然后javascript代码在内容div上运行。在这一点之后的任何php错误看不见或很难找到。我能够发送错误的电子邮件,这是我现在的调试方法 - 似乎不正确。
这是我当前的错误处理程序:
$message = "<p>Nice Job moron: A pos error occurred in script '$e_file' on line $e_line: $e_message\n<br />";
// Add the date and time:
$message .= "Date/Time: " . date('n-j-Y H:i:s') . "\n<br />";
// Append $e_vars to the $message:
$message .= "<pre>Error Variables:<br />" . print_r ($e_vars, 1) . "</pre>\n</p>";
// what is the url?
$message .= "<p><pre>URL: " .getPageURL() . "</pre>\n</p>";
$message .= '<pre>Session variables:<br />' . print_r($_SESSION,1). "</pre>\n</p>";
$message .= '<pre>Bactrace:<br />' . print_r(debug_backtrace(),1). "</pre>\n</p>";
if (!LIVE)
{
// Send an email to the admin:
mail(SUPPORT_EMAIL, 'TEST Site Error!', $message, 'From: ' . SUPPORT_EMAIL);
// Development (print the error).
echo '<div class="error">' . $message . '</div><br />';
}
else
{
// Send an email to the admin:
mail(SUPPORT_EMAIL, 'Site Error!', $message, 'From: ' . SUPPORT_EMAIL);
// Only print an error message if the error isn't a notice:
include(HEADER_FILE);
if ($e_number != E_NOTICE)
{
echo '<div class="error">A system error occurred. We apologize for the inconvenience.</div><br />';
}
echo '<div class="error">An Error has occurred and has been emailed to the administrator. Please note the conditions that caused this error and send a note to the administrator</div><br />';
}
这是我在问题How to show all PHP runtime errors in browser using Javascript的stackoverflow上找到的错误处理程序 我希望这可以工作,但事实并非如此。看起来脚本执行停止,并且没有发生html呈现。
//new error handling
$("#content").append('<p id="PHPError"> </p>');
$("#content").append('<button onclick="PHPErrors()">Show me all PHP runtime errors</button>');
var nerrors = 0; // PHP Errors count in Javascript
function PHPErrors()
{
// Javascript Function that wraps the PHP Code
<?php
// MyErrorHandler is my function that my custom error handler
// uses with error parameters in that given order.
function myErrorHandler($errno, $errstr, $errfile, $errline) {
echo 'ERror';
echo "document.getElementById('PHPError').innerHTML += " .
"\"$errno: $errstr ($errline)<br>\";\r\n";
echo "nerrors = nerrors + 1;\r\n";
return true;
}
// That's the line the set my custom new error-handling
// disabling the default error handling
$old_error_handler = set_error_handler("myErrorHandler");
// Below random PHP code full of errors
$b = $a; // PHP Error 1
$c = $a +1; // PHP Error 2
$d = $a + 1; // PHP Error 3
$e = $_SESSION['glove']; // PHP Error 4
?>
// No PHP runtime error deserves a nice messagen
if (nerrors===0)
{
document.getElementById('PHPError').innerHTML =
"Well done! PHP without errors.";
}
} // End Javascript Function
$(document).ready(function()
{PHPErrors()});
这是一个冗长的页面源输出。忽略我看到页眉和页脚输出的一些数据和函数,然后脚本开始。它在调用带有未定义变量的php函数后死亡。我还注意到我的脚本标签到处都是,可能是个问题。
<!DOCTYPE html >
<html lang="en">
<head>
<meta charset="utf-8">
<title>Create MFG Brand</title>
<link rel="stylesheet" href="https://www.embrasse-moi.com/POS_TEST/Engine/includes/css/style.css" type="text/css" media="all" />
<script>var POS_URL ='https://www.embrasse-moi.com/POS_TEST';</script><script>var LOGOUT_URL ='https://www.embrasse-moi.com/POS_TEST/Engine/login/logout.php';</script><script>POS_ENGINE_URL = "https://www.embrasse-moi.com/POS_TEST/Engine"</script>
<script type="text/javascript" src="https://www.embrasse-moi.com/POS_TEST/3rdParty/jQuery/jquery-1.11.2.min.js"></script>
<link type="text/css" href="https://www.embrasse-moi.com/POS_TEST/3rdParty/jQueryUI/jquery-ui-1.11.4.custom/jquery-ui.css" rel="Stylesheet" />
<script type="text/javascript" src="https://www.embrasse-moi.com/POS_TEST/3rdParty/jQueryUI/jquery-ui-1.11.4.custom/jquery-ui.min.js"></script>
<script src="https://www.embrasse-moi.com/POS_TEST/Engine/includes/javascript/javascript_library.2015.06.02.js"></script>
<script src="https://www.embrasse-moi.com/POS_TEST/Engine/includes/javascript/check_login.2013.02.16.js"></script>
</head>
<body style="background-color:rgb(255,255,125);">
<div id="header" class =></div>
<div id="content" onmousemove ="UserInput()" onclick="UserInput()" onkeypress="UserInput()" onscroll="UserInput()">
<script src="brand.2015.08.03.js"></script>
<script src="https://www.embrasse-moi.com/POS_TEST/Engine/includes/javascript/mySQLTable.2015.08.04.js"></script>
</div><div id="footer">
</div>
</body>
</html>
<script>
$("#content").append('<p id="PHPError"> </p>');
$("#content").append('<button onclick="PHPErrors()">Show me all PHP runtime errors</button>');
var nerrors = 0;
function PHPErrors()
{
ERrordocument.getElementById('PHPError').innerHTML += "8: Undefined variable: a (113)<br>";
nerrors = nerrors + 1;
ERrordocument.getElementById('PHPError').innerHTML += "8: Undefined variable: a (114)<br>";
nerrors = nerrors + 1;
ERrordocument.getElementById('PHPError').innerHTML += "8: Undefined variable: a (115)<br>";
nerrors = nerrors + 1;
ERrordocument.getElementById('PHPError').innerHTML += "8: Undefined index: glove (116)<br>";
nerrors = nerrors + 1;
if (nerrors===0) {
document.getElementById('PHPError').innerHTML =
"Well done! PHP without errors."; }
}
$(document).ready(function()
{PHPErrors()});
var type = "view";
var pos_manufacturer_id = 7;
var mfg_tbl_def = [json data sent by php];
var mfg_data = {json data from php};
var account_link = [json data from php];
var act_table_def = [json data from php];
var account_select_table_def = ERrordocument.getElementById('PHPError').innerHTML += "8: Undefined variable: table_object_name (285)<br>";
nerrors = nerrors + 1;