Security & Configuration

Managing Configuration and Secrets in PHP Applications

Keep environment-specific settings and credentials out of public directories and source control with a clear PHP configuration strategy.

Separate configuration from application logic

Avoid hardcoding database credentials, API keys, and environment-specific URLs in controllers or templates. Load configuration once through a bootstrap layer.

Keep secrets outside the document root

A hosting layout can place private configuration above public_html:

/home/account/
├── websiteconf/
│   └── app-config.php
└── public_html/
    ├── index.php
    └── assets/

The exact paths depend on the hosting account. The important rule is that secret files must not be directly downloadable through the web server.

Use environment variables where available

$dbHost = getenv('DB_HOST') ?: '127.0.0.1';
$dbName = getenv('DB_NAME');
$dbUser = getenv('DB_USER');
$dbPass = getenv('DB_PASSWORD');

Validate required values at startup and fail safely if configuration is incomplete.

Protect version control

Add local secret files to .gitignore. Provide a sanitized example configuration file containing placeholder values, never real credentials.

Rotate and restrict secrets

Use separate credentials for development and production, grant only required database permissions, and rotate credentials when exposure is suspected. Do not print secrets in error messages or logs.