Read settings from a .env file
Keep passwords out of your code. Put them in a file outside the web folder and read it once.
PHP · Read settings from a .env fileIntermediate
// /home/site/.env (outside public_html)
// DB_HOST=localhost
// DB_NAME=mydb
// APP_DEBUG=false
$env = parse_ini_file('/home/site/.env', false, INI_SCANNER_TYPED);
if ($env === false) {
http_response_code(500);
exit('Config missing');
}
$host = $env['DB_HOST'] ?? 'localhost';
$debug = (bool)($env['APP_DEBUG'] ?? false);configenvparse_ini_file