Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to secure database configuration file in project? [duplicate]

I have created on php file for establishing connection with database server. In this file, i am using mysql_connect() function with parameters host, username and password of my database server.

public class DatabaseConnect
{
function __construct()
{
    mysql_connect('localhost','username','password') or die('Could not connect to mysql server.');
    mysql_select_db('databasename');
}

}

Now in this case, username and password are visible to others.

I found one more way to secure the value i.e. mysql.default_user and mysql.default_password. In which scenario we have to this way?

Or how could i secure my values from others?

like image 616
Kalpana Dixit Avatar asked Feb 19 '13 08:02

Kalpana Dixit


People also ask

How do I protect my config file?

To secure passwords for configuration parameters, you can use an encrypted password file, separate from the configuration files. The pr0pass program maintains the password file, encrypting passwords for parameters in the configuration files.

What is config file in testing?

A configuration file, often shortened to config file, defines the parameters, options, settings and preferences applied to operating systems (OSes), infrastructure devices and applications in an IT context. Software and hardware devices can be profoundly complex, supporting myriad options and parameters.


2 Answers

Now in this case, username and password are visible to others.

Only to those, that have access to your source code. This should not be a problem. But if you want to separate code and database credentials, make sure that the configuration file is located outside the web root.

like image 21
Fabian Schmengler Avatar answered Sep 20 '22 05:09

Fabian Schmengler


You can try to put your database credentials in separate file with proper UNIX permissions set, for example 644, and then include this file on top of your script.

The configuration.php file will look like:

<?php
define (DB_USER, "mysql_user");
define (DB_PASSWORD, "mysql_password");
define (DB_DATABASE, "database_name");
define (DB_HOST, "localhost");
?>

Your original script will look something like this:

require ("configuration.php");
public class DatabaseConnect
{
function __construct()
{
    mysql_connect(DB_HOST,DB_USER,DB_PASSWORD) or die('Could not connect to MySQL server.');
    mysql_select_db(DB_DATABASE);
}

}
like image 87
Bud Damyanov Avatar answered Sep 22 '22 05:09

Bud Damyanov