Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to Read any Request Header in PHP
PHP provides several methods to read HTTP request headers, which contain important information about the client's request such as user agent, content type, and authentication details. Understanding how to access these headers is essential for web development tasks like content negotiation, security checks, and API development.
Using $_SERVER Superglobal
The most common method is using the $_SERVER superglobal array. Header names are prefixed with "HTTP_", converted to uppercase, and hyphens are replaced with underscores
<?php
// Reading specific headers
if (isset($_SERVER['HTTP_USER_AGENT'])) {
$userAgent = $_SERVER['HTTP_USER_AGENT'];
echo "User-Agent: " . $userAgent . "<br>";
} else {
echo "User-Agent header not found.<br>";
}
// Reading other common headers
$contentType = $_SERVER['HTTP_CONTENT_TYPE'] ?? 'Not set';
$authorization = $_SERVER['HTTP_AUTHORIZATION'] ?? 'Not set';
echo "Content-Type: " . $contentType . "<br>";
echo "Authorization: " . $authorization . "<br>";
?>
Using getallheaders() Function
The getallheaders() function returns all request headers as an associative array. This method preserves the original header name format
<?php
if (function_exists('getallheaders')) {
$headers = getallheaders();
foreach ($headers as $name => $value) {
echo "$name: $value<br>";
}
// Access specific header
$userAgent = $headers['User-Agent'] ?? 'Not available';
echo "User Agent: " . $userAgent . "<br>";
} else {
echo "getallheaders() function not available.<br>";
}
?>
Using apache_request_headers() Function
This function is Apache-specific and provides similar functionality to getallheaders()
<?php
if (function_exists('apache_request_headers')) {
$headers = apache_request_headers();
foreach ($headers as $name => $value) {
echo "$name: $value<br>";
}
} else {
echo "apache_request_headers() not available (not running on Apache).<br>";
}
?>
Creating a Universal Header Reader
For maximum compatibility across different server environments, you can create a function that tries multiple methods
<?php
function getAllRequestHeaders() {
// Try getallheaders() first
if (function_exists('getallheaders')) {
return getallheaders();
}
// Try apache_request_headers()
if (function_exists('apache_request_headers')) {
return apache_request_headers();
}
// Fallback to $_SERVER
$headers = array();
foreach ($_SERVER as $key => $value) {
if (strpos($key, 'HTTP_') === 0) {
$headerName = str_replace(' ', '-',
ucwords(str_replace('_', ' ',
strtolower(substr($key, 5)))));
$headers[$headerName] = $value;
}
}
return $headers;
}
// Usage
$allHeaders = getAllRequestHeaders();
foreach ($allHeaders as $name => $value) {
echo "$name: $value<br>";
}
?>
Common Use Cases
| Header | $_SERVER Key | Purpose |
|---|---|---|
| User-Agent | HTTP_USER_AGENT | Client browser/application info |
| Content-Type | HTTP_CONTENT_TYPE | Request body format |
| Authorization | HTTP_AUTHORIZATION | Authentication credentials |
| Accept | HTTP_ACCEPT | Preferred response format |
Conclusion
Reading request headers in PHP can be accomplished using $_SERVER, getallheaders(), or apache_request_headers(). The $_SERVER method offers the best compatibility across different server environments, while the other functions provide more convenient access when available.
