How can the security of database connection details be ensured in PHP applications?

To ensure the security of database connection details in PHP applications, it is recommended to store sensitive information such as database credentials in a separate configuration file outside of the web root directory. This helps prevent direct access to the credentials by unauthorized users. Additionally, using encryption techniques to secure the connection details 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_name');

// db_connect.php
include 'config.php';

$connection = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);

if (!$connection) {
    die("Connection failed: " . mysqli_connect_error());
}