← Home ← Codex ← DEBT
Browse by Category
+ added · updated 7d
← Back to glossary

.env Files & Environment Variables

PHP OWASP A2:2021 PHP 5.6+ Beginner
debt(d5/e3/b3/t7)
d5 Detectability Operational debt — how invisible misuse is to your safety net

Closest to 'specialist tool catches it' (d5). The detection_hints.tools list includes trufflehog, gitleaks, and semgrep — all specialist security scanning tools. These won't catch the problem by default in a normal dev workflow; a developer must deliberately run or configure these tools to detect committed secrets or missing .env.example files.

e3 Effort Remediation debt — work required to fix once spotted

Closest to 'simple parameterised fix' (e3). The quick_fix is: add .env to .gitignore, create .env.example with dummy values, and install vlucas/phpdotenv. This is a small, well-understood pattern fix involving a few files (gitignore, example file, composer.json), but not a deep refactor. If credentials were already committed, git history must be purged, which adds complexity — so e3 rather than e1.

b3 Burden Structural debt — long-term weight of choosing wrong

Closest to 'localised tax' (b3). The choice applies to both web and cli contexts and touches configuration management broadly, but it's a well-understood, low-ceremony pattern. Once set up correctly (.gitignore, .env.example, dotenv library), it imposes minimal ongoing burden. It doesn't shape architectural decisions for the rest of the codebase.

t7 Trap Cognitive debt — how counter-intuitive correct behaviour is

Closest to 'serious trap' (t7). The misconception field states explicitly: developers believe committing a dev .env is harmless, but it establishes a pattern that leads to production credentials being committed accidentally. This contradicts the developer's intuition ('dev credentials don't matter') and the failure mode is silent until a production credential leaks — a serious, non-obvious trap that many competent developers fall into.

About DEBT scoring →

Also Known As

.env file environment variables PHP vlucas/phpdotenv

TL;DR

Storing configuration in environment variables (not source code), loaded from a .env file in development via libraries like vlucas/phpdotenv.

Explanation

The Twelve-Factor App methodology mandates configuration in the environment — database credentials, API keys, and environment-specific settings should never be hardcoded or committed to version control. In development, vlucas/phpdotenv loads a .env file into $_ENV and getenv(). In production, variables are set at the infrastructure level (Docker, Kubernetes secrets, AWS Parameter Store). Always add .env to .gitignore; commit a .env.example with placeholder values. Access variables via $_ENV['KEY'] or getenv('KEY') — prefer $_ENV as it's not affected by php.ini's variables_order.

Watch Out

Never commit .env to version control — even a private repository. Use .env.example for documentation and secrets managers for production.

Common Misconception

Committing a .env file to the repository is fine if credentials are for a dev environment. .env files in version control establish a pattern that leads to production credentials being committed accidentally. Use .env.example with dummy values and keep real .env files out of git.

Why It Matters

Storing configuration in environment variables (twelve-factor app principle) keeps credentials out of source code and allows the same codebase to run in different environments without modification.

Common Mistakes

  • Committing the .env file to version control — the whole point is to keep secrets out of the repo.
  • Not providing a .env.example with all required keys — new developers don't know what to configure.
  • Loading .env in production when environment variables are already set by the server — causes double-loading conflicts.
  • Using $_ENV instead of getenv() or a dotenv library — $_ENV is not populated in all PHP configurations.

Code Examples

✗ Vulnerable
// .env committed to git — credentials exposed:
# .gitignore is missing .env entry
DB_PASSWORD=supersecret123
API_KEY=sk-live-abc123
# Anyone with repo access has these credentials
✓ Fixed
# .env — local secrets, NEVER committed to git
APP_ENV=production
DB_HOST=db.internal
DB_PASS=super_secret_password
STRIPE_SECRET=sk_live_...

# .env.example — template committed to git, no real secrets
APP_ENV=local
DB_HOST=localhost
DB_PASS=
STRIPE_SECRET=

# PHP — vlucas/phpdotenv
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
$dotenv->load();
$dotenv->required(['DB_HOST', 'DB_PASS', 'APP_KEY']); // fail fast if missing

$dbPass = $_ENV['DB_PASS'];

# In production: set env vars at server/container level — don't deploy .env files

Added 15 Mar 2026
Edited 22 Mar 2026
Views 176
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings T 0 pings W 2 pings T 1 ping F 1 ping S 0 pings S 1 ping M 1 ping T 1 ping W 2 pings T 1 ping F 0 pings S 2 pings S 2 pings M 0 pings T 2 pings W 0 pings T 1 ping F 1 ping S 0 pings S 0 pings M 1 ping T 1 ping W 0 pings T 2 pings F 1 ping S 2 pings S 1 ping M 1 ping T 0 pings W
No pings yet today
Perplexity 1
Perplexity 62 Amazonbot 19 Google 11 Scrapy 7 ChatGPT 6 Unknown AI 5 Ahrefs 5 SEMrush 5 Majestic 2 Claude 2 Bing 2 PetalBot 2 Meta AI 1
crawler 123 crawler_json 5 pre-tracking 1
DEV INTEL Tools & Severity
🟠 High ⚙ Fix effort: Low
⚡ Quick Fix
Use vlucas/phpdotenv to load .env in development; ensure .env is in .gitignore and .env.example documents all required keys without values
📦 Applies To
PHP 5.6+ web cli laravel symfony
🔗 Prerequisites
🔍 Detection Hints
.env file committed to git; missing .env.example; API keys in PHP config files instead of env vars
Auto-detectable: ✓ Yes trufflehog gitleaks semgrep
⚠ Related Problems
🤖 AI Agent
Confidence: High False Positives: Low ✗ Manual fix Fix: Medium Context: File
CWE-312 CWE-526


✓ schema.org compliant