What potential security risks are associated with storing MySQL connection details in a .inc file?
Storing MySQL connection details in a .inc file can pose a security risk because if the file is accessible to unauthorized users, they can easily view the sensitive information such as the username and password. To mitigate this risk, it is recommended to store the connection details in a separate PHP file outside of the web root directory and include it in your main script using include or require statements.
<?php
// db_config.php
$host = 'localhost';
$username = 'root';
$password = 'password';
$database = 'my_database';
?>
<?php
// main_script.php
require_once('/path/to/db_config.php');
// Connect to MySQL
$conn = new mysqli($host, $username, $password, $database);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
Related questions
-
PHP program execution background process
-
PHP game servers connection
-
PHP database integration website development