Skip to content
Learn Netverks

Lesson

Step 32/36 89% through track

security-basics

Web security fundamentals

Last reviewed May 28, 2026 Content v20260528
Track mode
server_script
Means
Server runner
Reading
~2 min
Level
advanced

This lesson

This lesson teaches Web security fundamentals: the syntax, APIs, and habits you need before advancing in PHP.

PHP’s popularity makes it a frequent target—validation, escaping, and prepared statements are baseline skills.

You will apply Web security fundamentals in contexts like: LAMP/LEMP stacks, Laravel apps, WordPress themes/plugins, and shared hosting.

Write PHP in the editor and click Run on server—the dev runner executes your script and returns stdout/stderr (set LEARNING_RUNNER_ENABLED=true locally).

When functions, arrays, and basic OOP from intermediate lessons are familiar.

Security is a set of habits, not a single library. PHP apps face the same threats as any web stack—prioritize the OWASP Top 10 basics.

Core defenses

  • SQL injection — prepared statements always for user data
  • XSS — escape on output (htmlspecialchars), Content-Security-Policy headers
  • CSRF — tokens on state-changing forms; SameSite cookies help
  • Session fixation/hijacking — regenerate session ID on login; HttpOnly + Secure cookies
  • Password storagepassword_hash / password_verify, never plain text or MD5

Password hashing

$hash = password_hash($password, PASSWORD_DEFAULT);
password_verify($password, $hash);

Secrets and config

Keep API keys and DB passwords in environment variables ($_ENV, getenv)—not in Git. Frameworks wrap this in .env files excluded from version control.

Important interview questions and answers

  1. Q: XSS vs SQL injection?
    A: XSS executes malicious scripts in victims' browsers; SQL injection manipulates database queries—fix with escaping/CSP vs prepared statements.
  2. Q: Why bcrypt/argon2 via password_hash?
    A: Adaptive cost slows brute force; salts are handled automatically.
  3. Q: Defense in depth?
    A: Layer validation, escaping, headers, least privilege DB users, and monitoring—no single fix is enough.

Self-check

  1. Where do you escape—on input or output?
  2. Why regenerate session ID after login?

Pitfall: Escape on output with context-aware functions—HTML, JSON, and URL encoding differ.

Interview prep

How to store passwords?

password_hash with PASSWORD_DEFAULT (bcrypt/argon2) and verify with password_verify—never MD5 or plain text.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • XSS escape where?
  • SQLi fix recap?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump