What are best practices for securely storing sensitive information like database credentials in PHP scripts?
Storing sensitive information like database credentials directly in PHP scripts can pose a security risk, as these credentials can be easily exposed if the script is accessed or compromised. To securely store these credentials, it is recommended to use environment variables or a separate configuration file outside of the web root directory.
// Define database credentials in a separate configuration file
$db_host = 'localhost';
$db_user = 'username';
$db_pass = 'password';
$db_name = 'database';
// Connect to the database using the defined credentials
$conn = new mysqli($db_host, $db_user, $db_pass, $db_name);
// Check the connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}