How can sensitive information, such as database passwords, be securely stored and accessed by a PHP script?
Sensitive information, such as database passwords, should never be hard-coded directly into PHP scripts as it poses a security risk. Instead, a common practice is to store these sensitive details in a separate configuration file outside of the web root directory. This file should be included in the PHP script to access the credentials securely.
// config.php
<?php
define('DB_HOST', 'localhost');
define('DB_USER', 'username');
define('DB_PASS', 'password');
define('DB_NAME', 'database_name');
?>
// script.php
<?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);
}
echo "Connected successfully";
?>