简体   繁体   English

如何保护包含数据库用户名和密码的配置文件

[英]How to secure configuration file containing database username and password

Issue问题

In order to connect my PHP code with MySQL database I use PDO way, creating variable, assigning it with new PDO object where arguments contain settings such as server, database, login and password.为了将我的 PHP 代码与 MySQL 数据库连接,我使用 PDO 方式,创建变量,为它分配新的 PDO 对象,其中参数包含服务器、数据库、登录名和密码等设置。 So in resulting code it could look like this:因此,在生成的代码中,它可能如下所示:

$DAcess=new PDO("mysql:host=server;dbname=database","login","password");

I don't feel comfortable having my login data written directly into the code nor do I find it effective in case of possible changes of those data.将我的登录数据直接写入代码让我感到不舒服,而且我认为在这些数据可能发生更改的情况下它也无效。 It was recommended to me to solve this by storing those data in other text file (preferably .INI file) from which it is going to be retrieved anytime I need, for example, having file:建议我通过将这些数据存储在其他文本文件(最好是 .INI 文件)中来解决这个问题,我可以随时从中检索它,例如,有文件:

xampp/htdoc/EXERCISE/secret/config.ini

The problem is If any user figures out the location and name of this file, they can easily access it and its content by entering URL/HTTP request into their browser:问题是,如果任何用户知道该文件的位置和名称,他们可以通过在浏览器中输入 URL/HTTP 请求轻松访问它及其内容:

server(localhost)/EXERCISE/secret/config.ini

It was adviced to me by the same source the file is supposed to be forbidden from acess by those protocols.这些协议应该禁止访问该文件的同一来源向我建议。 So I need to be able to acess the file with my PHP code but disallow any user to acess the directory/file on their own.因此,我需要能够使用我的 PHP 代码访问该文件,但不允许任何用户自行访问该目录/文件。 How to do this?这该怎么做?

Possible Solution可能的解决方案

I have been roaming these pages and other similar forumses yet all results of my research with keywords such as "forbidden" were about users who lost permission unintentionally.我一直在浏览这些页面和其他类似的论坛,但我使用关键字(如“禁止”)的所有研究结果都是关于无意中失去许可的用户。 I have also been looking for Google solution, yet Tutorials I have found were referencing to file located somewhere else in my XAMPP version and were about lines of settings not included in this file in my XAMPP version - considering I have downloaded XAMPP from official page, I should be having recent version, thus those tutorials were outdated.我也一直在寻找 Google 解决方案,但我发现的教程引用了位于我的 XAMPP 版本中其他位置的文件,并且是关于我的 XAMPP 版本中未包含在此文件中的设置行 - 考虑到我已经从官方页面下载了 XAMPP,我应该有最新版本,因此那些教程已经过时了。

It left me with no other choice but experiment on my own.它让我别无选择,只能自己尝试。 After a while, I have found directory "forbidden" in directory "htdoc", have played with those files and have ended up with something looking like solution to my issue.过了一会儿,我在目录“htdoc”中找到了目录“禁止”,使用了这些文件,最终得到了一些看起来像我的问题的解决方案。

Specifically, I copied .htacess (obviously nameless text file with but extension) and placed its copy into to-be-forbidden directory.具体来说,我复制了.htacess (显然是带有但扩展名的无名文本文件)并将其副本放入 to-be-forbidden 目录中。 I changed nothing in the file but line referencing to login data storing file.我没有更改文件中的任何内容,只是引用了登录数据存储文件的行。 I have created my own text file (nameless with but extension .ldatastore ) where using copied pattern login:password I have written my own desired login data and made .htacess use this file instead of original htdoc/forbidden/.htpassw .我创建了我自己的文本文件(无名但扩展名为.ldatastore ),其中使用复制的模式 login:password 我编写了自己想要的登录数据并使.htacess使用这个文件而不是原始的htdoc/forbidden/.htpassw

Since then, it seems it works.从那时起,它似乎有效。 Whenever I try to acces those files with my browser on new session (browser closed and opened again, otherwise it doesn't need autentification again), it does not let me browse the directory nor look into its files (neither those which are responsible for those actions such as .htacess or those I created myself such as config.ini ) unless I provide valid login data same to those in .ldatastore text file.每当我尝试在新会话中使用浏览器访问这些文件时(浏览器关闭并再次打开,否则它不再需要身份验证),它不会让我浏览目录或查看其文件(无论是那些负责那些操作(例如.htacess或我自己创建的操作(例如config.ini ))除非我提供与.ldatastore文本文件中的相同的有效登录数据。

So why am I asking this?那我为什么要问这个? I feel uncomfortable doing it this way because of several reasons listed below.由于下面列出的几个原因,我觉得这样做不舒服。 In case this is the only easy and possible solution, I can live with that, but in case there is much better way you would recommend, I will gladly read that, which is why I am asking for your suggestions.如果这是唯一简单可行的解决方案,我可以接受,但如果您推荐更好的方法,我会很乐意阅读,这就是我征求您的建议的原因。 I was also writing this whole text to explain my case fully, provide enough data and express "I have done some research and understanding of the case before asking" so that this would not be by the rules of this page marked as "off-topic".我也在写整篇文章来充分解释我的案例,提供足够的数据并表达“我在询问之前已经对案例进行了一些研究和理解”,这样就不会被本页面的规则标记为“偏离主题” ”。

Reasons Why I Would Prefer Alternative Solution我更喜欢替代解决方案的原因

  1. I feel like it is XAMPP framework dependant.我觉得它依赖于 XAMPP 框架。 That the whole module making this work is part of the framework's code while .htacess just marks the directories that should be forbidden by this module.完成这项工作的整个模块是框架代码的一部分,而 .htacess 只是标记了该模块应该禁止的目录。 That means I am afraid If I would release my project on proper paid server hosting with their own PHP executing software, it wouldn't work everywhere and that this is just XAMPP way to do it.这意味着我担心如果我使用他们自己的 PHP 执行软件在适当的付费服务器托管上发布我的项目,它不会在任何地方工作,而这只是 XAMPP 的方式来做到这一点。 Correct me If I am wrong and this is solution used widely on any PHP executioner.纠正我如果我错了,这是在任何 PHP 执行器上广泛使用的解决方案。

  2. I was trying to understand the module's documentation located as text file in the "forbidden" directory yet it seems from the documentation this module was developed mainly to make one safe and forbidden server storing secret data accessible then by various different application on different servers rather than just forbidding secret directory (I would leave this directory to be part of my application which is major difference between my usage and by author assumed usage).我试图理解位于“禁止”目录中作为文本文件的模块文档,但从文档看来,该模块的开发主要是为了使一个安全且禁止的服务器存储秘密数据,然后由不同服务器上的各种不同应用程序访问,而不是只是禁止秘密目录(我会将此目录保留为我的应用程序的一部分,这是我的用法和作者假设的用法之间的主要区别)。 Correct me If I am wrong and I misunderstood the usage.纠正我如果我错了,我误解了用法。

  3. Despite the fact I cannot acces the files via browser without login data, my PHP code seems to have no problem acessing the files - I used PHP code to retrieve text from text file that should be forbidden this way and it worked (it echoed the text) with no sign of problems.尽管我无法在没有登录数据的情况下通过浏览器访问文件,但我的 PHP 代码在访问文件时似乎没有问题 - 我使用 PHP 代码从文本文件中检索文本,该文本应该以这种方式被禁止并且它工作正常(它回显了文本) 没有任何问题。 Well, in the end, I certainly would like to make it work this way yet I expected even PHP code that retrieves the text would need to somehow contain login data to have access.好吧,最后,我当然想让它以这种方式工作,但我希望即使是检索文本的 PHP 代码也需要以某种方式包含登录数据才能访问。 This way it feels like anyone instead of entering the reference into browser would make their own PHP code that would acces those files from my server (which would make this act to increase security useless little bit).通过这种方式,感觉就像任何人而不是将引用输入浏览器都会制作自己的 PHP 代码,这些代码将从我的服务器访问这些文件(这会使这种提高安全性的行为变得毫无用处)。 Correct me If I am wrong and it is not this easy.纠正我如果我错了,这并不容易。

  4. I feel paranoid that it is not safe enough solution.我觉得这不是足够安全的解决方案。 Correct me If I am wrong and it is totally safe and preffered solution.纠正我如果我错了,这是完全安全和首选的解决方案。

Too Long, Didn't Read太长了,没看完

Is copying and pasting and customizing .htacess file safe enough to make directory forbidden only acessible by my PHP code to retrieve data from there and is it useable on most platforms?复制、粘贴和自定义 .htacess 文件是否足够安全,使禁止目录只能由我的 PHP 代码访问以从那里检索数据,并且它在大多数平台上都可用吗?

I have recently found in right bar of similar questions this one ( How to secure database configuration file in project? ), yet I am not sure whether it can be used in my case, too, and how to do so.我最近在类似问题的右侧栏中找到了这个( 如何保护项目中的数据库配置文件? ),但我不确定它是否也可以用于我的情况,以及如何使用。

As @Darkbee stated, the simplest way is to have the file outside your website root.正如@Darkbee 所说,最简单的方法是将文件放在网站根目录之外。 This would be accessible on the server, but not to the public under any circumstances.这可以在服务器上访问,但在任何情况下都不能对公众访问。

The alternative is to set the permissions to 400 on the file.另一种方法是将文件的权限设置为 400。

.htaccess could block access, but not blocking access to the server (which needs access) is just a long way of doing what would be simpler just using permissions. .htaccess 可以阻止访问,但不阻止对服务器(需要访问)的访问只是使用权限做更简单的事情的很长的路要走。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过用户名和密码访问安全区域 - how to access a secure area passing username and password PHP如何保护数据库中的密码 - PHP how to secure password in database 如何避免将数据库的用户名和密码存储在配置文件中? (PHP) - How to avoid storing the username and password of the database in a config file? (PHP) 有url用用户名和密码获取json数据,如何保护它,因为javascript源显示用户名和密码 - Got url to fetch json data with username and password, how to secure it as javascript source shows username and password 如何检查用户名和密码是否与数据库值匹配 - How to check username and password matches the database values 数据库中的用户名和密码未更新 - The username and password is not updating in Database 如何使用远程安全文件中的密码进行连接 - how to connect using a password in distant secure file 如何加密然后解密数据库类文件中使用的用户名和密码文本 - how do I encrypt and then decrypt the text of username and password used in database class file php用户名和密码验证安全代码 - php username and password validation secure code Phpass-如何根据数据库中的用户名和密码哈希检查登录用户名和密码 - Phpass - how to check login username and password against username and password hash in database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM