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


当前回答

我们是这样解决的:

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.

其他回答

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

我们是这样解决的:

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.

有些人把这个问题误解为如何在数据库中存储密码。这是错误的。它是关于如何存储密码,让您进入数据库。

通常的解决方案是将密码从源代码移到配置文件中。然后,将管理和保护配置文件的工作交给系统管理员。这样,开发人员就不需要知道任何关于产品密码的信息,并且在源代码控制中也没有密码记录。

如果你在别人的服务器上托管,在你的webroot之外没有访问权限,你可以把你的密码和/或数据库连接放在一个文件中,然后使用.htaccess锁定文件:

<files mypasswdfile>
order allow,deny
deny from all
</files>

对于非常安全的系统,我们在配置文件中加密数据库密码(该文件本身由系统管理员保护)。在应用程序/服务器启动时,应用程序会提示系统管理员输入解密密钥。然后从配置文件中读取数据库密码,解密并存储在内存中以供将来使用。仍然不是100%安全,因为它存储在内存中解密,但你必须在某些时候称它“足够安全”!