Send the right HTTP status code

Search engines and APIs read the status code. Do not return 200 for an error page.

PHP · Send the right HTTP status codeIntermediate
function fail(int $code, string $message): never
{
    http_response_code($code);
    header('Content-Type: text/plain; charset=utf-8');
    echo $message;
    exit;
}

// 404 page not found
$post = null;
if (!$post) {
    fail(404, 'Page not found');
}
// 403 forbidden, 401 not logged in, 405 wrong method, 429 too many requests, 503 maintenance
header('Retry-After: 3600');          // use with 429 / 503
httpstatusheader