2011-10-28 108 views
3

在早期它的一些点出现有太多的SQL连接其次为回显筛查的任何人一直在寻找:错误,使用PDO连接到mysql的连接太多,显示我们的db用户名和密码?

PHP Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[08004] [1040] Too many connections' in /dir/file.php:21 
Stack trace: 
#0 /dir/file.php(21): PDO->__construct('mysql:host=loca...', 'the user', 'password') 

正如你可以看到,它打印的用户名和密码。

我是新来的PDO连接到数据库的形式,我进入它,因为人们告诉我,我的常规mysql_connect形式是不安全的。但是,默认情况下,当mysql_connect有太多的连接时,永远不会将用户名和密码输出到屏幕上。

真是一场灾难。

如果出现错误,我该如何停止PDO?

我使用:

$dbh = new PDO('mysql:host=localhost;dbname=' . $db, $user, $pass); 
+0

你有没有碰巧图这一个出来? – hafichuk

+2

我同意这是一个问题,应该提交给PDO团队。请在出现错误时投票隐藏凭据:http://bugs.php.net/bug.php?id=62184 – IMB

+2

好的 - 会的。虽然看起来很奇怪,甚至应该有投票的需要 - 它只是在默认打印用户名和密码凭证的错误信息荒谬!尤其是当显然,至少我对PDO的理解是为了更安全地访问数据库!超级失败。 – willdanceforfun

回答

2

编辑php.ini并设定

display_errors = 0 

如果您没有访问到你的脚本的顶部,然后php.ini中(S )您需要添加:

ini_set("display_errors", "0"); 

http://php.net/manual/en/errorfunc.configuration.phphttp://php.net/manual/en/function.error-reporting.php

这将停止输出到浏览器的所有错误。确保你仍然在记录错误(通过php.ini设置)。

P.S.不要扔掉PDO

+3

编号例外。 –

+0

@ TomalakGeret'kal我完全同意,但是您仍然想要禁用输出到浏览器作为预防措施。 – hafichuk

+0

感激不尽!谢谢。 – willdanceforfun

3

PDO没有做任何事情。

发生这种情况是因为您没有执行任何异常处理,因此将调用默认的PHP错误处理程序并显示您的调用堆栈,包括函数参数。

通常调用堆栈是非常好的事情,所以你想让的错误处理程序工作;你只需要正确使用它。

  • 地方try/catch在可能引发异常的代码周围的相关位置块;至少,在代码的顶层放置一个全面的try/catch块,以便没有任何东西可以渗透。

PDO文件,虽然明确指出(在一个红色警告框)的大风险,你拿不正确捕获PDO例外:

If your application does not catch the exception thrown from the PDO constructor, the default action taken by the zend engine is to terminate the script and display a back trace. This back trace will likely reveal the full database connection details, including the username and password. It is your responsibility to catch this exception, either explicitly (via a catch statement) or implicitly via set_exception_handler().

http://www.php.net/manual/en/pdo.connections.php

+0

这对我的智能版本是有意义的。现在,如果我可以努力工作,那么尝试/抓住什么。感谢指针! – willdanceforfun

+0

@cosmicbdog:在使用使用它们的库之前,您绝对需要了解异常情况! –

+0

这也有道理:) – willdanceforfun