Why put MySQL credentials outside of www directory? [duplicate]

别等时光非礼了梦想. 提交于 2019-12-22 07:11:02

问题


Possible Duplicate:
Putting core classes above the web root - good or bad idea?

I keep reading that it's a best practice to put MySQL connection credentials (whether it's a class, defines, etc..) outside of the web root (above the www folder).

Why is this? If the credentials are in a .php file then it doesn't matter if the file is accessible through the browser, right?


回答1:


It's a preventative measure. If someone accidently disables php evaluation in your apache server or changes an apache setting in an .htaccess file, the file could be served up like any plain text file. Or, if you accidently forget a php start tag, it would be redenerd like plain text. Not that you'd make such a dumb mistake, but maybe a future newbie working on your code might make a mistake.

Why leave a possible vector open when you can prevent it from ever being possible? Just take the advice of others who have shot their own foot (or like me, shot both feet and a hand) and move the credentials outside your docroot.




回答2:


Because, to a certain extent, nothing under the web root is secure. It's available on the Internet, which makes it inherently insecure.

There always exists the possibility that a misconfigured server may one day output the contents of any PHP file, rather than send it to PHP to be interpreted. There are also too many people out there trying to get into your database any way they can... some of them just for fun.

In any given situation, you should always use the most secure methods available. It's a good habit to get into.

Also, you should never use the root password in your web application. Create a special user with minimal privileges.

For PHP I always use an .ini file to store sensitive configurations...

<?php
    $config = parse_ini_file('../config.ini');
?>



回答3:


Because what's inside www directory (ie the root of your website) can be potentially accessible from the internet.

That means that if you put your credentials in here, maybe someone will be able to access them, and to connect directly to your DB.

Putting your credentials outside of this directory guarantees that it won't be accessible this way.



来源:https://stackoverflow.com/questions/13976401/why-put-mysql-credentials-outside-of-www-directory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!