Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the most accepted method for hiding password for 'connect.php' file?

As my server is getting a bit bigger, and more users are getting access to it, I don't want them to see the password that MySQL is using to connect to PHP, which is stored in my 'connect.php' file and required by every page. However, it is just sitting in the same directory as the rest of the php files.

I've considered using a second 'connect.php'-like file with access to only one table, that stores the encrypted passwords to connect to MySQL, but then I would have the problem of hiding the key to it.

Changing permissions won't work either, if you chmod o-r or something similar, nobody will be able to access the web application, obviously.

Is there an accepted method to get around this problem, or should I just solve it on my own? The problem is that I don't want it to be too convoluted if there is an accepted method.

like image 664
Ryan Ward Avatar asked Jun 08 '11 16:06

Ryan Ward


3 Answers

I would strongly recommend moving connect.php in one directory above your DOCUMENT_ROOT so that it is not accessible from your web server.

Your php files can of course include connect.php with full or relative path eg:

require_once('../connect.php');
like image 184
anubhava Avatar answered Sep 20 '22 17:09

anubhava


All the answers have good advice but fail to address the fact that any user with server access can just snoop around and open the config.php in an editor.

Set your config files in a directory outside of public webspace , the webserver should be the owner of this directory and it should have permissions set to 700. All files it contains should be 644. This way no one can even read the file contents apart from webserver user or root.

This is a common approach, but there is a lot more to the subject as security is a very vast topic, but is better than 90% of the setups out there.

like image 39
stefgosselin Avatar answered Sep 17 '22 17:09

stefgosselin


Set $password, connect, then unset() $password. They should be never able to recover it. I don't think a PHP file can be downloaded anyway, neither seen. It is always compiled by the server before.

like image 41
Shoe Avatar answered Sep 18 '22 17:09

Shoe