debugging 显示所有错误和警告[duplicate]

vmpqdwk3  于 2022-11-14  发布在  其他
关注(0)|答案(6)|浏览(230)

此问题在此处已有答案

How do I get PHP errors to display?(31个答案)
三年前就关门了。

更新2:

现在我已经从.php文件中删除了以下内容:

<?php error_reporting( E_ALL ); ?>

我在php.ini中设置了display_erros,如下所示:
显示错误=打开
错误报告在php.ini中设置为以下值:
错误报告= E_ALL|严格
重新启动Apache后,我仍然没有收到任何错误/警告。

更新1:

我已将php.ini中的error_reporting从:
错误报告= E_ALL & ~E_已弃用

错误报告= E_ALL|严格
之后,我重新启动Apache,例如。
/etc/初始化.d/apache 2重新启动
但该页面仍不会显示任何类型的错误/警告。

原始问题:

下面的脚本生成了一个警告,因为$err在if语句中。为什么这个警告没有显示在Web浏览器的PHP页面上?
我必须查看Apache日志才能看到警告。还有,如果我故意将“insert into”更改为“delete into”,它在PHP页面上不会显示错误。为什么错误不会显示在实际的PHP页面上?

<?php
    error_reporting(E_ALL);
?>

<html>
    <head>
        <title></title>
        <link rel="icon" type="image/png" href="favicon.ico">

        <?php
            if ($_SERVER['REQUEST_METHOD'] == 'POST') {
                $err = array();

                if (empty( $_POST['display_name']))
                    $err[] = "display name field is required";
                if (empty( $_POST['email']))
                    $err[] = "email field is required";
                if (empty( $_POST['password']))
                    $err[] = "password field is required";

                if (!$err) {
                    try {
                        $DBH = new PDO("mysql:host=localhost;dbname=database1", "user", "pass");
                        $DBH->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

                        $STH = $DBH->prepare("delete into table1 (display_name, email, password) values ( :display_name, :email, :password )");

                        $STH->bindParam(':display_name', $_POST['display_name'], PDO::PARAM_STR, 100);
                        $STH->bindParam(':email', $_POST['email'], PDO::PARAM_STR, 100);
                        $STH->bindParam(':password', $_POST['password'], PDO::PARAM_STR, 100);

                        $STH->execute();

                        $STH = $DBH->prepare("delete into table2 ( username, status, users_id ) values ( :username, :status, :users_id )");

                        $strStatus = 1;

                        $STH->bindParam(':username', $_POST['display_name'], PDO::PARAM_STR, 100);
                        $STH->bindParam(':status', $strStatus, PDO::PARAM_INT, 1);
                        $STH->bindParam(':users_id', $_POST['referer'], PDO::PARAM_INT, 1);

                        $STH->execute();

                        $DBH = null;
                    }
                    catch (PDOException $e) {
                        echo $e->getMessage();
                    }

                    header("Location: " . $_SERVER['PHP_SELF']);
                    exit;
                }
                else {
                    foreach ($_POST as $key => $val) {
                        $form[$key] = htmlspecialchars($val);
                    }
                }
            }
            else {
                $form['display_name'] = $form['email'] = $form['password'] = '';
            }
        ?>
    </head>

    <body>
        <?php foreach($err as $line) { ?>
        <div style="error"><?php echo $line; ?></div>
        <?php } ?>

        <h1>Register</h1>

        <form method="post">
            Referers id:<br/>
            <input type="text" name="referer" /><br/><br/>

            Name:<br/>
            <input type="text" name="display_name" value="<?php echo $form['display_name']; ?>" /><br/><br/>

            Email:<br/>
            <input type="text" name="email" value="<?php echo $form['email']; ?>" /><br/><br/>

            Password:<br/>
            <input type="text" name="password" value="<?php echo $form['password']; ?>" /><br/><br/>

            <input type="submit" value="register" />
        </form>
    </body>
</html>
zdwk9cvp

zdwk9cvp1#

可以在php.ini或Apache配置文件中关闭显示错误。
您可以在脚本中启用它:

error_reporting(E_ALL);
ini_set('display_errors', '1');

您应该会在PHP错误日志中看到相同的消息。

ttygqcqt

ttygqcqt2#

我能够通过以下代码获得所有错误:

ini_set('display_startup_errors', 1);
ini_set('display_errors', 1);
error_reporting(-1);
vc9ivgsu

vc9ivgsu3#

直接从 * php.ini * 文件中下载:

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Error handling and logging ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

; This directive informs PHP of which errors, warnings and notices you would like
; it to take action for. The recommended way of setting values for this
; directive is through the use of the error level constants and bitwise
; operators. The error level constants are below here for convenience as well as
; some common settings and their meanings.
; By default, PHP is set to take action on all errors, notices and warnings EXCEPT
; those related to E_NOTICE and E_STRICT, which together cover best practices and
; recommended coding standards in PHP. For performance reasons, this is the
; recommend error reporting setting. Your production server shouldn't be wasting
; resources complaining about best practices and coding standards. That's what
; development servers and development settings are for.
; Note: The php.ini-development file has this setting as E_ALL. This
; means it pretty much reports everything which is exactly what you want during
; development and early testing.
;
; Error Level Constants:
; E_ALL             - All errors and warnings (includes E_STRICT as of PHP 5.4.0)
; E_ERROR           - fatal run-time errors
; E_RECOVERABLE_ERROR  - almost fatal run-time errors
; E_WARNING         - run-time warnings (non-fatal errors)
; E_PARSE           - compile-time parse errors
; E_NOTICE          - run-time notices (these are warnings which often result
;                     from a bug in your code, but it's possible that it was
;                     intentional (e.g., using an uninitialized variable and
;                     relying on the fact it is automatically initialized to an
;                     empty string)
; E_STRICT          - run-time notices, enable to have PHP suggest changes
;                     to your code which will ensure the best interoperability
;                     and forward compatibility of your code
; E_CORE_ERROR      - fatal errors that occur during PHP's initial startup
; E_CORE_WARNING    - warnings (non-fatal errors) that occur during PHP's
;                     initial startup
; E_COMPILE_ERROR   - fatal compile-time errors
; E_COMPILE_WARNING - compile-time warnings (non-fatal errors)
; E_USER_ERROR      - user-generated error message
; E_USER_WARNING    - user-generated warning message
; E_USER_NOTICE     - user-generated notice message
; E_DEPRECATED      - warn about code that will not work in future versions
;                     of PHP
; E_USER_DEPRECATED - user-generated deprecation warnings
;
; Common Values:
;   E_ALL (Show all errors, warnings and notices including coding standards.)
;   E_ALL & ~E_NOTICE  (Show all errors, except for notices)
;   E_ALL & ~E_NOTICE & ~E_STRICT  (Show all errors, except for notices and coding standards warnings.)
;   E_COMPILE_ERROR|E_RECOVERABLE_ERROR|E_ERROR|E_CORE_ERROR  (Show only errors)
; Default Value: E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED
; Development Value: E_ALL
; Production Value: E_ALL & ~E_DEPRECATED & ~E_STRICT
; http://php.net/error-reporting
error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT

对于纯开发,我选择:

error_reporting = E_ALL ^ E_NOTICE ^ E_WARNING

另外,不要忘记打开display_errors

display_errors = On

然后,在Ubuntu上重新启动Apache服务器:

sudo /etc/init.d/apache2 restart
y0u0uwnf

y0u0uwnf4#

PHP错误可以通过以下任何方法显示:

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

如需详细信息:


ioekq8ef

ioekq8ef5#

php.ini上设置以下内容:

;display_startup_errors = On
display_startup_errors=off
display_errors =on
html_errors= on

在PHP页面中,使用适当的过滤器来报告错误。

error_reporting(E_ALL);

过滤器可根据需要制作。

E_ALL
E_ALL | E_STRICT
j8yoct9x

j8yoct9x6#

您可以查看详细说明here

ini_set('display_errors', 1);

// Report simple running errors
error_reporting(E_ERROR | E_WARNING | E_PARSE);

// Reporting E_NOTICE can be good too (to report uninitialized
// variables or catch variable name misspellings ...)
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);

// Report all errors except E_NOTICE
error_reporting(E_ALL & ~E_NOTICE);

// Report all PHP errors (see changelog)
error_reporting(E_ALL);

// Report all PHP errors
error_reporting(-1);

// Same as error_reporting(E_ALL);
ini_set('error_reporting', E_ALL);

更改日志

  • 5.4.0 E_STRICT成为E_ALL的一部分
  • 5.3.0引入了已弃用的E_和已弃用的E_USER_。
  • 5.2.0引入了E_RECOVERABLE_ERROR(可恢复错误)。
  • 5.0.0引入了E_STRICT(不是E_ALL的一部分)。

相关问题