2011-03-25 92 views
270

更新2:显示所有错误和警告

我现在已经移除了PHP文件如下:在php.ini

<?php error_reporting(E_ALL); ?> 

我已经设置display_erros如下:

display_errors = On

错误报告在php.ini中设置为以下内容

error_reporting = E_ALL | E_STRICT

重新启动apache后,我仍然没有错误/警告。

UPDATE 1:

我已经改变了从在php.ini使用error_reporting:

的error_reporting = E_ALL & 〜E_DEPRECATED

error_reporting = E_ALL | E_STRICT

之后我重新启动apache,

/etc/init.d/apache2重启

,但是网页仍然不会显示任何类型的错误/警告。

原题:

因为$ ERR if语句里面是下面的脚本生成警告。为什么这个警告不会显示在网页浏览器的php页面上?我必须看看Apache日志才能看到警告。此外,如果我delibrately更改“插入到”“删除到”,它不会显示错误的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> 
+1

什么是['display_errors'](http://php.net/display_errors)INI设置的当前值? – salathe 2011-03-25 21:00:05

+0

我刚刚检查并将其设置为error_reporting = E_ALL&〜E_DEPRECATED – oshirowanen 2011-03-25 21:05:06

+0

请重新阅读我的问题,我们已经知道您的'error_reporting'设置,因为它在脚本中。 – salathe 2011-03-25 21:15:13

回答

773

显示错误可能在php.ini或您的Apache配置文件中被关闭。

您可以在脚本中打开它:

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

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

+1

我需要吗?在更改php.ini文件后重新启动apache? – oshirowanen 2011-03-25 21:10:08

+1

已经发布了这个问题http://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display – Elangovan 2014-12-03 11:55:44

+2

@oshirowanen'ini_set()'实际上并没有改变'php。 ini'文件中,脚本只会像“php.ini”中那样设置“行为”... **从手册:**'设置给定配置选项的值。配置选项将在脚本执行过程中保留这个新值'*',在脚本结尾处将恢复'**'。' – 2014-12-08 22:04:55

34

直接从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 

而且在那之后重新启动服务器,在Ubuntu阿帕奇不要忘了把的display_errors对

display_errors = On 

sudo /etc/init.d/apache2 restart 
+3

“PHP 6.0.0”? heh :-) – Bell 2016-10-29 00:02:47

5

set t hese on 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 
17

我可以通过下面的代码来获取所有错误: -

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

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

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

有关详细信息:

http://www.howtofindit.in/displaying-php-errors/

+4

PHP文件中的'display_errors = on;'有'语法错误'。 – 2017-07-23 15:38:02