Module 30: PHP Security – AdvancedAdvanced60 min read
Advanced PHP Security: Defense in Depth, XSS/SQLi/CSRF, IDOR & Security Headers
Master production PHP security: trust boundaries, password_hash/verify, htmlspecialchars, PDO prepared statements, CSRF tokens, IDOR authorization checks, and HTTP security headers.
Key Takeaways & Core Concepts
- SQL Injection: Always use PDO prepared statements with
ATTR_EMULATE_PREPARES => false. - XSS: Encode HTML output with
htmlspecialchars($val, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8'). - Passwords: Never use MD5/SHA1/SHA256! Always use
password_hash($pw, PASSWORD_ARGON2ID)(orPASSWORD_BCRYPT) andpassword_verify(). - IDOR (Insecure Direct Object Reference): Verify resource ownership (
WHERE id = ? AND user_id = ?) on every request!
Advanced PHP Security: Defense in Depth, XSS/SQLi/CSRF, IDOR & Security Headers
### 1. Defense in Depth & OWASP Top 10 in PHP
- SQL Injection: Always use PDO prepared statements with
ATTR_EMULATE_PREPARES => false. - XSS: Encode HTML output with
htmlspecialchars($val, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8'). - Passwords: Never use MD5/SHA1/SHA256! Always use
password_hash($pw, PASSWORD_ARGON2ID)(orPASSWORD_BCRYPT) andpassword_verify(). - IDOR (Insecure Direct Object Reference): Verify resource ownership (
WHERE id = ? AND user_id = ?) on every request! - Security Headers:
X-Content-Type-Options: nosniff,X-Frame-Options: DENY,Content-Security-Policy.
1. Argon2id/Bcrypt Password Hashing & Verification
1<?php
2$plain = "CodeZov@2026!";
3$hash = password_hash($plain, PASSWORD_BCRYPT);
4echo "Verified: " . (password_verify($plain, $hash) ? "YES" : "NO") . "\n";
Click code or "Edit Code" to edit
Interactive Code Block•Tab = 4 spaces•Ctrl + Enter = Run
4 lines
Note: `password_hash()` automatically generates a cryptographically random salt.
Practice & AssessmentPHP Programming Tutorial
Test Your Understanding
Reinforce core concepts with interactive multiple-choice questions, live coding challenges, and top technical interview questions.
Your Current Score
0 / 0 correct(0 attempted)