Build baseline controls into every request: HTTPS, cookie hardening, CSRF, safe output, parameterized SQL, backups and dependency updates.
The examples introduce the underlying control before showing a verification and an application-level safeguard.
What You Will Build
Build baseline controls into every request: HTTPS, cookie hardening, CSRF, safe output, parameterized SQL, backups and dependency updates.
Prerequisites
Use PHP 8.4 or newer, HTTPS for all authentication operations, and an application session or database where required. Example configuration values are placeholders, never production secrets.
Example 1: Basic Operation
Create a response-specific CSP nonce and use it in both header and script.
<?php
$nonce = rtrim(strtr(base64_encode(random_bytes(16)), '+/', '-_'), '=');
header("Content-Security-Policy: default-src 'self'; script-src 'self' 'nonce-{$nonce}'; object-src 'none'; base-uri 'self'");
echo '<script nonce="' . htmlspecialchars($nonce, ENT_QUOTES, 'UTF-8') . '">console.log("Ready")</script>';
Example 2: A Different Use Case
Apply baseline response security headers without using unsafe-inline.
<?php
header('X-Content-Type-Options: nosniff');
header('Referrer-Policy: strict-origin-when-cross-origin');
header("Content-Security-Policy: default-src 'self'; object-src 'none'; frame-ancestors 'none'");
Practical Example: Checking the Result
Protect authentication cookies and sensitive pages alongside CSP.
<?php
session_set_cookie_params(['secure' => true, 'httponly' => true, 'samesite' => 'Lax']);
// Call before session_start(), under HTTPS.
header('Cache-Control: no-store');
Important Behavior and Edge Cases
A checklist is a starting point, not an audit; severity depends on data sensitivity and deployment architecture.
Enforce controls on the server, test denial paths and do not assume that a snippet alone implements an entire production workflow.
Comparison with Related Functions
| Approach | Key distinction |
|---|---|
| PHP Security Checklist for Small Websites | Build baseline controls into every request: HTTPS, cookie hardening, CSRF, safe output, parameterized SQL, backups and dependency updates. |
| Related technique | Rate limiting, least privilege and monitoring complement application-level input checks. |
Common Beginner Mistakes
| Mistake | Correct approach |
|---|---|
| Confusing this operation with a neighboring cryptographic or security mechanism | Rate limiting, least privilege and monitoring complement application-level input checks. |
| Ignoring a security prerequisite or failure case | A checklist is a starting point, not an audit; severity depends on data sensitivity and deployment architecture. |
Key Takeaways
- Build baseline controls into every request: HTTPS, cookie hardening, CSRF, safe output, parameterized SQL, backups and dependency updates.
- A checklist is a starting point, not an audit; severity depends on data sensitivity and deployment architecture.
- Rate limiting, least privilege and monitoring complement application-level input checks.
Apply this behavior to a real application only after confirming the required extension, configuration and failure handling on the deployment runtime.