Application Security

Configure Security Headers for PHP Websites

Understand useful HTTP security headers and set them consistently without breaking application functionality.

Set headers before output

header('X-Content-Type-Options: nosniff');
header('Referrer-Policy: strict-origin-when-cross-origin');
header('X-Frame-Options: SAMEORIGIN');

Use a Content Security Policy tailored to the scripts and assets your site actually needs. Test it carefully; do not copy a restrictive policy without checking application behavior.

Enable HSTS only after HTTPS is ready

header('Strict-Transport-Security: max-age=31536000');

Only enable HSTS when the domain is consistently served over HTTPS. includeSubDomains and preload have broader consequences and should be chosen deliberately.

Use CSP as defense in depth

Prefer nonces or hashes for necessary inline scripts rather than allowing all inline execution. A report-only policy can help identify compatibility problems before enforcement.

Test response headers

Inspect responses in browser developer tools or with an HTTP client. Verify login, forms, embedded resources, and third-party integrations still work.

Security headers complement secure coding; they do not fix SQL injection or broken authorization.