Is there a way to hide all database info such as password, username, etc?

30
April 16, 2019, at 03:00 AM

I'm creating several php scripts. Must I always insert the host, username, password, etc. for every script for the same database and table? Is there a way to make reference with only one line or some other way? Is there anyway to hide this info?

$host="XXXXXXXXX";
$username="XXXXX";
$password="XXXXX";
$db_name="XXXXXX";
$tbl_name="XXXXX";
mysql_connect("$host", "$username", "$password") or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
Answer 1

While PHP files are executed and thus the source code is not visible from the web, an accidental misconfiguration could change this. You could put the DB configuration in a separate file outside the wbeserver's document root directory and use PHP's require command to include it in the other scripts.

However, depending on the PHP configuration, files outside the docroot may not be accessible to PHP scripts, but there are ways around this. This SO question discusses these issues in detail

Answer 2

If you don't want to put the credentials in php files then you can put them the php.ini configuration file.

mysql.default_host = "localhost"
mysql.default_user = "user"
mysql.default_password = "pass"

then in the php source:

<?php
$connect_db = mysql_connect();
$err = mysql_error();
if ($err != "")
{
        echo "Error connecting to database: $err";
        die;
}
if (!mysql_select_db("mydomain_com_test", $connect_db))
{
        echo mysql_error()."<br/>";
        die;
}
$sql = "SELECT NOW()";
$rows = mysql_query( $sql );
?>
READ ALSO
SQL filter out ids that have multiple values

SQL filter out ids that have multiple values

I have MySQL table in the following format

32
How I skip one cell in MySQL query

How I skip one cell in MySQL query

I'm trying to make PHP code update column called "downloads" to zero inside MySQL

16
need help for query code export from 2table (by php) in mysql

need help for query code export from 2table (by php) in mysql

i need code for export favs in tablei make 2 table in mysql one : tbl_post two : tbl_markshow

32
Implementing Auto-Bidding Functionality with JSP/MySQL

Implementing Auto-Bidding Functionality with JSP/MySQL

Currently working on a project and could use some advice for how to approach implementing an Auto-Bidding system for a web application similar to Ebay

24