What are the potential security risks of storing login credentials in a PHP script?
Storing login credentials in a PHP script poses a significant security risk because if an attacker gains access to the script, they can easily retrieve the credentials. To mitigate this risk, it is recommended to store the credentials in a separate configuration file outside of the web root directory.
// config.php
<?php
define('DB_HOST', 'localhost');
define('DB_USER', 'username');
define('DB_PASS', 'password');
define('DB_NAME', 'database');
?>
// index.php
<?php
require_once('config.php');
// Use the defined constants for database connection
$conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
?>
Related Questions
- What best practices should be followed when combining data from different sources in PHP, such as inserting OpenGeoDB information into a MySQL database for efficient processing?
- What are the potential pitfalls of using preg_match for email validation in PHP?
- How can you accurately count page breaks in a string using PHP?