How can forum administrators protect sensitive MySQL data from being exposed in PHP code?
To protect sensitive MySQL data from being exposed in PHP code, forum administrators can store the database credentials in a separate configuration file outside of the web root directory. This way, even if the PHP code is compromised, the database credentials remain secure. Additionally, using secure coding practices such as input validation and parameterized queries can help prevent SQL injection attacks.
// config.php
define('DB_HOST', 'localhost');
define('DB_USER', 'username');
define('DB_PASS', 'password');
define('DB_NAME', 'database_name');
// connection.php
require_once('config.php');
$conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}