PHP headers_list Function
last modified April 4, 2025
The PHP headers_list
function retrieves the list of response headers
to be sent to the client. It's useful for debugging and header inspection.
Basic Definition
headers_list
returns an array of headers prepared for sending.
These include both PHP-generated and manually set headers using header().
Syntax: headers_list(): array
. Returns a numerically indexed array.
Headers are only available after output buffering starts or before output.
Basic Header Inspection
This example shows how to retrieve and display all response headers.
<?php declare(strict_types=1); header('Content-Type: text/html; charset=utf-8'); header('X-Powered-By: PHP/8.1'); $headers = headers_list(); print_r($headers);
This displays all headers set for the response, including Content-Type. The output shows both system-generated and custom headers.
Checking for Specific Headers
This demonstrates how to check if a specific header has been set.
<?php declare(strict_types=1); header('Cache-Control: no-cache'); function has_header(string $name): bool { foreach (headers_list() as $header) { if (stripos($header, $name) === 0) { return true; } } return false; } echo has_header('Cache-Control') ? 'Cache header set' : 'No cache header';
This checks if Cache-Control header exists in the response headers. The function searches for headers starting with the specified name.
Header Modification Detection
This example shows how to detect if headers have been modified.
<?php declare(strict_types=1); $initial_headers = headers_list(); header('X-Custom-Header: Value'); header_remove('X-Powered-By'); $current_headers = headers_list(); $modified = $initial_headers != $current_headers; echo $modified ? 'Headers changed' : 'Headers unchanged';
This compares headers before and after modifications to detect changes. Useful for middleware that needs to verify header modifications.
Content-Type Validation
This shows how to validate the Content-Type header before output.
<?php declare(strict_types=1); function validate_content_type(string $expected): bool { foreach (headers_list() as $header) { if (stripos($header, 'Content-Type:') === 0) { return stripos($header, $expected) !== false; } } return false; } header('Content-Type: application/json'); echo validate_content_type('json') ? 'Valid' : 'Invalid';
This ensures the response Content-Type matches expectations before sending. Particularly useful in API implementations requiring specific formats.
Header Debugging Tool
This example creates a simple header debugging tool for development.
<?php declare(strict_types=1); header('X-Debug: Enabled'); header('Cache-Control: no-store'); function debug_headers(): string { $output = "<h3>Response Headers</h3><ul>"; foreach (headers_list() as $header) { $output .= "<li>" . htmlspecialchars($header) . "</li>"; } return $output . "</ul>"; } echo debug_headers();
This creates an HTML list of all response headers for debugging purposes. Helpful during development to verify header configuration.
Best Practices
- Timing: Call before any output to get complete headers
- Performance: Avoid excessive calls in production
- Security: Don't expose sensitive headers to end users
- Testing: Use for unit testing header configurations
Source
PHP headers_list Documentation
This tutorial covered the PHP headers_list
function with practical
examples for header inspection, validation, and debugging in PHP applications.