PHP header Function
last modified April 4, 2025
The PHP header
function sends raw HTTP headers to the client.
It must be called before any actual output is sent to the browser.
Basic Definition
header
sends HTTP headers to the browser before any content.
Headers control caching, redirects, content types, and more.
Syntax: header(string $header, bool $replace = true, int $response_code = 0): void
.
The function must be called before any output is generated.
Basic Redirect Example
This example demonstrates a simple page redirect using the header function.
<?php declare(strict_types=1); header("Location: https://www.example.com/newpage.php"); exit;
This sends a 302 redirect header to the browser. The exit statement prevents further script execution. Always include exit after Location headers.
Setting Content Type
This shows how to set different content types for various file formats.
<?php declare(strict_types=1); // For JSON response header('Content-Type: application/json'); // For plain text // header('Content-Type: text/plain'); // For XML // header('Content-Type: application/xml'); echo json_encode(['status' => 'success', 'message' => 'Data loaded']);
Setting proper content types ensures browsers interpret responses correctly. Different APIs require specific content types for proper functionality.
Forcing File Download
This example forces the browser to download a file instead of displaying it.
<?php declare(strict_types=1); $filename = 'report.pdf'; $filepath = '/path/to/files/' . $filename; header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename="' . basename($filename) . '"'); header('Content-Length: ' . filesize($filepath)); readfile($filepath); exit;
This sends headers to trigger a download dialog. The Content-Disposition header specifies the filename. Always include Content-Length for large files.
Setting Cache Control
This demonstrates how to control browser caching with HTTP headers.
<?php declare(strict_types=1); // Prevent caching header("Cache-Control: no-cache, no-store, must-revalidate"); header("Pragma: no-cache"); header("Expires: 0"); // Or set future expiration // header("Cache-Control: public, max-age=86400"); // header("Expires: " . gmdate('D, d M Y H:i:s', time() + 86400) . ' GMT'); echo "This content has specific caching rules applied";
Cache headers control how browsers store responses. Different scenarios require different caching strategies for optimal performance.
Setting HTTP Status Codes
This example shows how to send different HTTP status codes.
<?php declare(strict_types=1); // 404 Not Found header("HTTP/1.0 404 Not Found"); // 500 Internal Server Error // header("HTTP/1.0 500 Internal Server Error"); // 301 Moved Permanently // header("HTTP/1.1 301 Moved Permanently"); // header("Location: /new-url.php"); echo "Error: Page not found"; exit;
HTTP status codes communicate request results to clients. Proper status codes help with SEO and API response handling.
Best Practices
- Output Buffering: Use ob_start() to prevent header errors
- Early Execution: Call header() before any output
- Exit After Redirect: Always exit after Location headers
- Content-Type: Always set appropriate content types
- Status Codes: Use correct HTTP status codes
Source
This tutorial covered the PHP header
function with practical
examples for HTTP header manipulation in various scenarios.