Skip to content
Learn Netverks

Lesson

Step 26/36 72% through track

headers-redirects

Headers and redirects

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

This lesson

This lesson teaches Headers and redirects: the syntax, APIs, and habits you need before advancing in PHP.

Teams ship Headers and redirects on every PHP codebase—skipping it leaves gaps in debugging and code reviews.

You will apply Headers and redirects 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 you can explain the previous lesson's ideas without copying starter code.

HTTP responses include a status line, headers, and body. PHP sends headers with header() before any output—whitespace or echo first causes "headers already sent" errors.

Common headers

  • Content-Type: application/json for APIs
  • Cache-Control for caching policy
  • Location with 302/303 for redirects after POST

Redirect after POST

header('Location: /thanks.php', true, 303);
exit;

Use exit after redirects so no further output runs. PRG pattern (Post-Redirect-Get) prevents duplicate form submissions on refresh.

Playground note

header() affects real HTTP responses—not terminal output here. Learn the sequence; verify on local server.

Self-check

  1. Why must headers precede body output?
  2. What problem does Post-Redirect-Get solve?

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

  • Headers already sent?
  • 302 vs 303?

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