PHP Reference ยท Concepts and Reference
PHP security checklist
A practical PHP security checklist: validate input, escape output, use prepared statements, hash passwords, protect sessions and forms, and harden configuration.
Security in PHP comes down to a short list of habits: never trust input, escape output for the place it lands, keep secrets out of code, and use the built-in tools instead of inventing your own. This checklist covers the most common vulnerabilities and shows the safe version of each.
Treat it as a review list before launch, not as a guarantee. A real project also needs dependency updates, logging and a plan for incidents.
<?php // Validate on the way in, escape on the way out. $email = filter_var($_POST['email'] ?? '', FILTER_VALIDATE_EMAIL); echo htmlspecialchars($name, ENT_QUOTES, 'UTF-8');
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
The checklist
| Risk | What to do |
|---|---|
| SQL injection | PDO or MySQLi prepared statements with bound parameters. Never build SQL by concatenating input. |
| Cross-site scripting (XSS) | Escape on output with htmlspecialchars() (or your template engine's auto-escaping). Use the right escaping for HTML, attributes, JS and URLs. |
| Cross-site request forgery (CSRF) | A per-session token in every state-changing form, plus SameSite cookies. |
| Passwords | password_hash() and password_verify(). Never md5(), sha1() or plain hash(). Rehash with password_needs_rehash(). |
| Sessions | session_regenerate_id(true) after login, HttpOnly, Secure and SameSite cookie flags, short lifetimes. |
| File uploads | Check size, real MIME type with finfo, generate your own file names, store outside the web root. |
| Command execution | Avoid exec() and friends. If needed, use escapeshellarg() and an allow-list. |
| Includes and paths | Never include or open a path built from input. Use basename() and allow-lists. |
| Unserialize | Never call unserialize() on untrusted data. Use JSON. |
| Randomness | random_bytes() and random_int() for tokens and secrets, not rand() or mt_rand(). |
| Secrets | Keep them in environment variables or a vault, never in the repository. |
| Errors | display_errors=Off in production, log to a file. |
| Transport | HTTPS everywhere, HSTS, redirect plain HTTP. |
| Dependencies | Run composer audit and update regularly. |
Usage notes
- Validate (is this acceptable?) and escape (is this safe here?) are separate steps. Validation does not replace escaping.
- Escape for the destination, not the source. The same string is escaped differently for HTML, SQL, shell and JSON.
filter_var()validates formats; it is not a complete sanitizer.FILTER_SANITIZE_STRINGis deprecated.- Compare secrets with
hash_equals()to avoid timing leaks. - Check authorization on every request on the server, not just by hiding buttons.
- Web-specific features (sessions, headers, cookies) cannot run in this CLI sandbox, so the examples above stick to functions that work here.