How can one securely store database access information in a PHP application?

To securely store database access information in a PHP application, it is recommended to use environment variables or a configuration file outside of the web root directory. This helps prevent unauthorized access to sensitive information. Additionally, consider encrypting the database credentials if possible.

// Example of storing database access information in environment variables
$db_host = getenv('DB_HOST');
$db_username = getenv('DB_USERNAME');
$db_password = getenv('DB_PASSWORD');
$db_name = getenv('DB_NAME');

// Connect to the database using the retrieved credentials
$conn = new mysqli($db_host, $db_username, $db_password, $db_name);

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}