当PHP应用程序建立数据库连接时,当然通常需要传递登录名和密码。如果我对我的应用程序使用一个最小权限的登录,那么PHP需要知道这个登录名和密码。保护密码的最好方法是什么?只在PHP代码中编写它似乎不是一个好主意。


当前回答

一个额外的技巧是使用一个PHP单独的配置文件,如下所示:

<?php exit() ?>

[...]

Plain text data including password

这并不妨碍您正确地设置访问规则。但如果你的网站被黑客攻击,“要求”或“包括”将在第一行退出脚本,因此更难获得数据。

然而,不要让配置文件在可以通过web访问的目录中。你应该有一个“Web”文件夹,包含你的控制器代码,css,图片和js。这是所有。其他的都放在脱机文件夹中。

其他回答

只是把它放在配置文件的某个地方是通常完成的方式。只要确保你:

禁止从网络外的任何服务器访问数据库, 注意不要意外地向用户显示密码(在错误消息中,或通过PHP文件意外地作为HTML提供,等等)。

将它们存储在web根目录外的一个文件中。

如果谈论的是数据库密码,而不是来自浏览器的密码,标准的做法似乎是将数据库密码放在服务器上的PHP配置文件中。

您只需要确保包含密码的php文件具有适当的权限即可。也就是说,它应该是可读的只有web服务器和您的用户帐户。

是否可以在存储凭据的同一文件中创建数据库连接。将凭据内联到connect语句中。

mysql_connect("localhost", "me", "mypass");

否则最好在connect语句之后取消设置凭据,因为不在内存中的凭据不能从内存中读取;)

include("/outside-webroot/db_settings.php");  
mysql_connect("localhost", $db_user, $db_pass);  
unset ($db_user, $db_pass);  

我们是这样解决的:

Use memcache on server, with open connection from other password server. Save to memcache the password (or even all the password.php file encrypted) plus the decrypt key. The web site, calls the memcache key holding the password file passphrase and decrypt in memory all the passwords. The password server send a new encrypted password file every 5 minutes. If you using encrypted password.php on your project, you put an audit, that check if this file was touched externally - or viewed. When this happens, you automatically can clean the memory, as well as close the server for access.