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

心已入冬 提交于 2019-12-05 11:11:53

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.

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');
?>

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.

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