PHP运行时错误

时间:2013-01-15 22:38:21

标签: php runtime-error xdebug

我正在编程PHP一段时间。

我使用 Windows XP SP3 PHP 5.4 SQlite 3 Apache 2.4 作为开发环境。 远程主机服务器(不是我的)是 Windows Server 2008 PHP 5.3 Apache 2.2 IIS 7.0 SQL Server 2008

我的编辑器简单而精彩 EditPlus

关于 Javascript ,因为 Firefox 加上强大的附加功能 FireBug ,它很不错,它允许完整的HTML + CSS + Javascript调试。

我的问题是我的开发环境(我的个人计算机)中的PHP错误管理。

PHP语法错误通常在行号等情况下显示正常,因此可以轻松修复。还有用于检查语法的在线工具(例如pilliap)和其他用于测试PHP代码段执行的在线工具(例如writecodeonline.com

但是,我的诅咒是运行时错误(例如,未初始化的变量 $ _SESSION )。有时它显示正确,但有时错误消息(包括正确的行号!)嵌入在源代码中,在虚假的HTML标记内。简单测试显示没问题。但我的真实代码通常会这样做。

这不是阻止我前进的情况,但这是令人不快的。

我没有在互联网上找到关于这种奇怪行为的任何暗示,如果有人可以提供一些线索,我很好奇。

我的问题:浏览器中有时会出现PHP运行时错误(根据需要在开发环境中),有时它会在Javascript + HTML源代码中出现错误的HTML标记吗?

PS:PHP.Ini文件中的PHP日志文件或显示错误设置不是问题。

PS2:安装或不安装XDebug时都会出现同样的情况。

5 个答案:

答案 0 :(得分:2)

使用php error_log() function以您希望的方式显示有关错误的信息。

除此之外,请查看您的php.ini配置文件。有许多变量(html_errorserror_logdisplay_errorserror_reporting ...)可以根据需要设置错误输出,因此不会弄乱html输出。

答案 1 :(得分:1)

这是log_errors和错误日志文件的用途。只需阅读它们就可以很好地列出所有错误。


编辑:您还可以使用以下内容拖动页面上的所有错误:

首先,将error_prepend_stringerror_append_string设置为明显的值。例如,您可以使用<phperrormessage></phperrormessage>。任何不太可能出现在合法输出中的东西。

然后将其放在代码的开头:

ob_start(function($c) {
    if( preg_match_all("(<phperrormessage>(.*?)</phperrormessage>)is",$c,$matches)) {
      $c = preg_replace("(<phperrormessage>(.*?)<phperrormessage>)is","",$c);
      $c = str_replace(
          "</body>",
          "<div id=\"phperrors\">".implode("<hr />",$matches[1])."</div></body>",
          $c);
    }
    return $c;
});

答案 2 :(得分:0)

论坛和Q&amp; A网站确实是一个很好的学习资源!感谢 Kolink StackOverflow.com

Kolink 提示中,我进行了一些研究,然后我创建了一种新方法来驯服PHP中的绝大多数运行时错误。

我还没弄清楚究竟发生了什么,但我发现了一种不使用默认PHP错误处理的强大方法,而是在同一个地方收集所有运行时错误消息。

请注意,PHP编译错误对我来说不是问题,因为任何语法检查器都会解决它,包括PHP.exe。

假设我有一个PHP源代码,其中包含许多潜在的运行时错误。所以我将我的代码放在HTML标记内的以下行中,例如在我需要使用PHP代码的初始位置。

 <script type="text/javascript">
 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 "document.getElementById('PHPErro').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

在此代码之后,可以自由地添加其他Javascript和PHP。

最后我在HTML体内部分放了两个这两行。

    <p id="PHPError"> </p>
    <button onclick="PHPErrors()">Show me all PHP runtime errors</button> 

当我浏览这个PHP页面时,我可以看到这个按钮。所以我按它,我可以看到所有PHP运行时错误消息或令人愉快的“做得好......”消息。

答案 3 :(得分:0)

最后我发现了PHP会发生什么。

默认的PHP错误处理会在正文部分中生成错误。使用 XDebug ,消息更加详细,但同样的情况也会发生。

因此,当我将PHP代码放在JS函数中时,在BODY部分之前,此行为不起作用,因为BODY部分之前的HTML标记会生成垃圾。

语法错误是不同的,因为它是PHP解释器的前一个阶段,它是在运行PHP + HTML代码之前完成的。

因此,为了能够检查错误,在这种情况下,您需要查看源代码。将出现带有错误消息的虚假标签。

因此,我在Q&A中发布的其他StackOverflow代码处理了这种情况。

答案 4 :(得分:0)

我有同样的问题。

我尝试了你的解决方案但收效甚微。我其实喜欢我的原始错误处理程序 - 它发送一封电子邮件。当我以前用PHP构建页面时,我发现我不需要发送电子邮件,因为错误会很好地显示出来。现在我正在转向html的javascript渲染,错误没有显示或隐藏。登录有点痛苦导航到,页面源更好,但我的电子邮件总是打开。它会构建一条错误消息并通过电子邮件发送消息以及echo消息,但正如我们所知,这是倾向于页面源并且没有显示或有时显示为位。

function pos_error_handler ($e_number, $e_message, $e_file, $e_line, $e_vars) 
{


$includes = get_included_files();
//var_dump( $includes);
//echo 'WTF';
// Build the error message.
$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 />';
}
//include(FOOTER_FILE);
exit();
}