What are the best practices for securely storing connection details to an external database in a PHP script?
Storing connection details to an external database securely in a PHP script is crucial to prevent unauthorized access to sensitive information. One best practice is to store the connection details in a separate configuration file outside of the web root directory and restrict access to it. Additionally, using constants to define the connection details and using PHP's built-in functions like `password_hash()` to securely store passwords can enhance security.
<?php
// config.php
define('DB_HOST', 'localhost');
define('DB_USER', 'username');
define('DB_PASS', 'password');
define('DB_NAME', 'database_name');
// db.php
include('config.php');
$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
if ($mysqli->connect_error) {
die('Connection failed: ' . $mysqli->connect_error);
}