Security for PHPs
Security in PHP is the habit of treating every input as hostile. It covers preventing SQL injection, cross-site scripting through output escaping, CSRF with tokens, secure session handling, safe password storage with password_hash, and strict validation of file uploads by type, size and storage location.
Stage: Secure and Ship · Part of the PHP roadmap · Last reviewed 19 November 2026
What to learn in Security
- Escape output with htmlspecialchars to prevent cross-site scripting
- Hash and verify passwords using password_hash and password_verify
- Add and check CSRF tokens on state-changing forms
- Configure session cookies with httponly, secure and samesite flags
- Validate uploaded files by MIME type and size and store them outside the web root
Security interview questions
How should passwords be stored in PHP?
Use password_hash with the default algorithm, which applies a salt and a cost factor, and check logins with password_verify. Never use md5 or sha1 alone. Call password_needs_rehash after login to upgrade old hashes.
Why is trusting the file extension of an upload unsafe?
Attackers can name a PHP file photo.jpg or upload a script with a harmless extension. Check the real MIME type, restrict allowed types, rename the file, and store it outside the web root or in a location where scripts cannot execute.
Where Security fits in the PHP roadmap
Security belongs to the Secure and Ship stage (intermediate level), which takes about 14 hours. Quality.