Take the 2-minute tour ×
Information Security Stack Exchange is a question and answer site for Information security professionals. It's 100% free, no registration required.

I would know if it is secure to centralize database information into one file like this :

db_infos.php:

$server = "localhost";
$user = "root";
$password = "1234";
$database = "foo";
$table = "bar";

And then, include this file when I need to:

include("db_infos.php');
mysql_connect($server, $user, $password);
...

Is there any way to get the database information, else is there other ways more secured or more relevant (both are accepted ;p )?

If it's not secure please tell me why, and how it will :)

share|improve this question

3 Answers 3

up vote 6 down vote accepted

I'd say it is as secure as the directory it is placed it.

The ideal place to store the database connection file would be outside the web root. In this way, an attacker would not be able to gain access to the file using directory traversal attacks.

More information on directory traversal: http://en.wikipedia.org/wiki/Directory_traversal_attack

This page also has more information on the subject: http://stackoverflow.com/questions/6322766/php-basics-where-to-store-mysql-password-used-by-php

share|improve this answer
    
Can you please tell me more precisely why does it depends of the directory's security? Do you speak about chmod? –  torr Jun 5 '12 at 5:18
    
@torr I updated my answer with more info :) –  Terry Chia Jun 5 '12 at 5:24
    
Thanks a lot, I will leave that as not responded in order to have more advices about that. :) –  torr Jun 5 '12 at 5:28
    
"as secure as..." I assume you are ignoring the plaintext password?? –  AviD Jul 16 '13 at 20:23

Firstly, live by the least privilege rule. Unless you explicitly need to write to the database on a given page, use a read only user.

I generally try not to ever hardcode the credentials into the source code. It always just seemed wrong (even on a server side language). Rather, I put them into a restricted read-only configuration file that's outside of the root directory. I believe you can store the connection details inside your httpd.conf or virtual hosts file (if your host grants you access to it), this way you can call mysql_connect without any parameters, which will default to reading from the configuration.

Lastly, configure your server to only accept connections from the server and local user itself and block everything else. Do not display the credentials on error outputs. Better yet, disable errors all together and test changes locally before up you make them live. MySQL has a tendency to spit out valuable information

share|improve this answer
    
It is usually a good idea to disallow TCP connections altogether and use Unix sockets, which require the web server to have filesystem access to the socket (i.e. the database server places a socket in the web server's chroot) and allow for authentication without a password (the database server can query which user and process is connecting). –  Simon Richter Jun 5 '12 at 10:43

Yes, it is OK to centralize the connection information and credentials for the database into a single configuration file.

However, do not store that file under your web root. Make sure you store it elsewhere outside the web root. There are two reasons for this. First, if you're not careful, someone might be able to access that file through their browser, and then they learn everything needed to connect to your database. Second, if you're not careful, your editor might save backup copies of the file, again causing it to be exposed to the world (see here for more detail).

Also, do not check this file into the source code version control system. You don't want your passwords and crypto keys exposed in that version control system, with copies kept forever and backed up persistently.

For more information, see the following questions on this site: Is it a secure way to declare DB parameters in htaccess rather than in a PHP file?, where to store a key for encryption, Should a closed source website keep a secret key in its source?, and How do open source projects handle secure artifacts?.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.