How can PHP scripts be structured to prevent exposing sensitive information in the source code?
To prevent exposing sensitive information in PHP scripts, one approach is to store sensitive information such as database credentials or API keys in a separate configuration file outside of the web root directory. This way, the sensitive information is not directly accessible to external users who might view the source code.
// config.php
<?php
define('DB_HOST', 'localhost');
define('DB_USER', 'username');
define('DB_PASS', 'password');
define('DB_NAME', 'database_name');
?>
// 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
-
password hashing password_verify bcrypt
-
PHP DateTime::createFromFormat() date conversions
-
time calculations formatting PHP libraries