Skip to content
Justice Tech School

Headers, Redirects and Status Codes

header() sends HTTP headers before any output. Use it for redirects, content types and status codes, then exit. Output sent earlier causes headers already sent errors. Shown as code since it needs a web server.

Example
<?php
header("Content-Type: application/json");
http_response_code(201);
echo json_encode(["ok" => true]);

// redirect
// header("Location: /login");
// exit;

Exercise

Send a header.

("Location: /")

Quiz Test

When must header() be called?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.