显示所有错误和警告
更新 2:
我现在已从 .php 文件中删除了以下内容:
<?php error_reporting( E_ALL ); ?>
我已在 php.ini 中设置了 display_erros,如下所示:
显示错误=开
错误报告在 php.ini 中设置为以下内容:
错误报告 = E_ALL | E_STRICT
重新启动 Apache 后,我仍然没有收到任何错误/警告。
更新 1:
我已将 php.ini
中的 error_reporting 更改为:
error_reporting = E_ALL & ~E_DEPRECATED
至
错误报告 = E_ALL | E_STRICT
之后我重新启动了 Apache,例如
/etc/init.d/apache2 重新启动
但页面仍然不会显示任何类型的错误/警告。
原始问题:
以下脚本生成警告,因为 $err 位于 if 语句内。为什么此警告没有显示在 Web 浏览器的 PHP 页面上?
我必须查看 Apache 日志才能看到警告。另外,如果我故意将“插入”更改为“删除”,它不会在 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>
UPDATE 2:
I have now removed the following from the .php file:
<?php error_reporting( E_ALL ); ?>
I have set display_erros in php.ini as follows:
display_errors = On
Error reporting is set to the following in php.ini
:
error_reporting = E_ALL | E_STRICT
After restarting Apache, I still get no errors/warnings.
UPDATE 1:
I have changed error_reporting in php.ini
from:
error_reporting = E_ALL &
~E_DEPRECATED
to
error_reporting = E_ALL | E_STRICT
After which I restarted Apache, e.g.
/etc/init.d/apache2 restart
But the page will still not display errors/warnings of any kind.
ORIGINAL QUESTION:
The following script is generating a warning because the $err being inside the if statement. Why is this warning not being displayed on the PHP page in a web browser?
I have to look at the Apache logs to see the warning. Also, if I deliberately change the "insert into" to "delete into", it does not display an error on the PHP page. Why are the errors not displaying on the actual PHP page?
<?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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
在
php.ini
上设置这些:在 PHP 页面中,使用合适的过滤器进行错误报告。
锉刀可根据要求制作。
Set these on
php.ini
:From your PHP page, use a suitable filter for error reporting.
Filers can be made according to requirements.
您可以在此处查看详细说明。
引入了 5.2.0 E_RECOVERABLE_ERROR。
引入 5.0.0 E_STRICT(不是 E_ALL 的一部分)。
You can see a detailed description here.
可以在 php.ini 或 Apache 配置文件中关闭显示错误。
您可以在脚本中打开它:
您应该在 PHP 错误日志中看到相同的消息。
Display errors could be turned off in the
php.ini
or your Apache configuration file.You can turn it on in the script:
You should see the same messages in the PHP error log.
我能够通过以下代码获取所有错误:
I was able to get all errors via the below code:
PHP 错误可以通过以下任何一种方法显示:
有关更多详细信息:
显示 PHP 错误< /a>
PHP errors can be displayed by any of below methods:
For more details:
Displaying PHP errors
直接来自 php.ini 文件:
对于纯粹的开发,我会选择:
也不要忘记将 display_errors 设置为 on
之后,在 Ubuntu 上重新启动 Apache 服务器:
Straight from the php.ini file:
For pure development I go for:
Also don't forget to put display_errors to on
After that, restart your server for Apache on Ubuntu: