当PHP应用程序建立数据库连接时,当然通常需要传递登录名和密码。如果我对我的应用程序使用一个最小权限的登录,那么PHP需要知道这个登录名和密码。保护密码的最好方法是什么?只在PHP代码中编写它似乎不是一个好主意。
当前回答
一个额外的技巧是使用一个PHP单独的配置文件,如下所示:
<?php exit() ?>
[...]
Plain text data including password
这并不妨碍您正确地设置访问规则。但如果你的网站被黑客攻击,“要求”或“包括”将在第一行退出脚本,因此更难获得数据。
然而,不要让配置文件在可以通过web访问的目录中。你应该有一个“Web”文件夹,包含你的控制器代码,css,图片和js。这是所有。其他的都放在脱机文件夹中。
其他回答
一个额外的技巧是使用一个PHP单独的配置文件,如下所示:
<?php exit() ?>
[...]
Plain text data including password
这并不妨碍您正确地设置访问规则。但如果你的网站被黑客攻击,“要求”或“包括”将在第一行退出脚本,因此更难获得数据。
然而,不要让配置文件在可以通过web访问的目录中。你应该有一个“Web”文件夹,包含你的控制器代码,css,图片和js。这是所有。其他的都放在脱机文件夹中。
之前我们将DB user/pass存储在一个配置文件中,但后来进入了偏执模式——采用了深度防御策略。
如果您的应用程序被泄露,用户将拥有对您的配置文件的读访问权,因此黑客就有可能读取这些信息。配置文件也可能在版本控制中被捕获,或者在服务器中被复制。
我们已经切换到在Apache VirtualHost中设置的环境变量中存储user/pass。这个配置只能由根用户读取——希望您的Apache用户不是以根用户身份运行。
这样做的缺点是,现在密码在一个全局PHP变量中。
为了降低这种风险,我们有以下预防措施:
The password is encrypted. We extend the PDO class to include logic for decrypting the password. If someone reads the code where we establish a connection, it won't be obvious that the connection is being established with an encrypted password and not the password itself. The encrypted password is moved from the global variables into a private variable The application does this immediately to reduce the window that the value is available in the global space. phpinfo() is disabled. PHPInfo is an easy target to get an overview of everything, including environment variables.
有些人把这个问题误解为如何在数据库中存储密码。这是错误的。它是关于如何存储密码,让您进入数据库。
通常的解决方案是将密码从源代码移到配置文件中。然后,将管理和保护配置文件的工作交给系统管理员。这样,开发人员就不需要知道任何关于产品密码的信息,并且在源代码控制中也没有密码记录。
我们是这样解决的:
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.
将数据库密码放在一个文件中,使其对提供文件的用户只读。
除非您有某种方法只允许php服务器进程访问数据库,否则这几乎就是您所能做的全部工作。
推荐文章
- 获得PostgreSQL数据库中当前连接数的正确查询
- MySQL数据库表中的最大记录数
- 原则-如何打印出真正的sql,而不仅仅是准备好的语句?
- 如何从关联PHP数组中获得第一项?
- PHP/MySQL插入一行然后获取id
- 从现有模式生成表关系图(SQL Server)
- 我如何排序一个多维数组在PHP
- 如何在PHP中截断字符串最接近于一定数量的字符?
- PHP错误:“zip扩展名和unzip命令都没有,跳过。”
- Nginx提供下载。php文件,而不是执行它们
- HyperLogLog算法是如何工作的?
- Json_encode()转义正斜杠
- 数据库和模式的区别
- 如何在PHP中捕获cURL错误
- JavaScript:客户端验证与服务器端验证