Are there specific best practices for handling database access credentials in PHP applications?
Storing database access credentials securely is crucial to prevent unauthorized access to sensitive data in PHP applications. One best practice is to store credentials in a separate configuration file outside of the web root directory and restrict access to it. Another approach is to use environment variables to store credentials and access them in the PHP application.
// config.php
define('DB_HOST', 'localhost');
define('DB_USER', 'username');
define('DB_PASS', 'password');
define('DB_NAME', 'database_name');
```
```php
// db_connection.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);
}