Skip to content
PHP Logo

What potential pitfalls can arise from directly embedding database connection details in PHP files instead of using a centralized file?

Embedding database connection details in PHP files can lead to security risks as the credentials are exposed in plain text. It also makes it difficult to manage and update connection details across multiple files. To mitigate these risks, it's recommended to use a centralized configuration file where database connection details are stored securely.

php
// config.php

define('DB_HOST', 'localhost');
define('DB_USER', 'username');
define('DB_PASS', 'password');
define('DB_NAME', 'database_name');

// db.php

require_once 'config.php';

$connection = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);

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