What are the best practices for managing database access credentials in PHP applications?
Managing database access credentials securely is crucial to prevent unauthorized access to sensitive data. One best practice is to store credentials in a separate configuration file outside of the web root directory. This helps to prevent direct access to the file by malicious users. Additionally, using secure methods such as encryption or hashing to store and retrieve credentials can add an extra layer of protection.
// config.php
define('DB_HOST', 'localhost');
define('DB_USER', 'username');
define('DB_PASS', 'password');
define('DB_NAME', 'database');
// db.php
require_once('config.php');
$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
if ($mysqli->connect_error) {
die("Connection failed: " . $mysqli->connect_error);
}