Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to secure database passwords in PHP?

When a PHP application makes a database connection it of course generally needs to pass a login and password. If I'm using a single, minimum-permission login for my application, then the PHP needs to know that login and password somewhere. What is the best way to secure that password? It seems like just writing it in the PHP code isn't a good idea.

like image 715
user18359 Avatar asked Sep 18 '08 23:09

user18359


People also ask

Is it safe to store password in php file?

The best way is to store password above your root directory. If you decide to have password in php file then no body would able to view because php files are excuted in the server. But if the server does not support php then those files will be delivered as text files and any one can see the password.

Is it safe to store passwords in database?

Storing plain text passwords in the database is a sin. Encryption functions provide one-one mapping between input and output and they are always reversible. If the hacker gets the key, he will be able to decrypt the passwords. The better way would be to use a one way cryptographic hash function.

How do you secure a database connection?

Identify the user IDs that you want to associate with the database connection, or create a user ID with a password, following the appropriate instructions for your operating system and database. Define the user IDs and passwords that the integration node can use to access a particular data source.


2 Answers

Several people misread this as a question about how to store passwords in a database. That is wrong. It is about how to store the password that lets you get to the database.

The usual solution is to move the password out of source-code into a configuration file. Then leave administration and securing that configuration file up to your system administrators. That way developers do not need to know anything about the production passwords, and there is no record of the password in your source-control.

like image 60
user11318 Avatar answered Sep 23 '22 06:09

user11318


If you're hosting on someone else's server and don't have access outside your webroot, you can always put your password and/or database connection in a file and then lock the file using a .htaccess:

<files mypasswdfile> order allow,deny deny from all </files> 
like image 32
kellen Avatar answered Sep 26 '22 06:09

kellen