How to Get Headers in PHP
-
Use the
get_headers()
to Get Headers of a Given URL in PHP -
Use
$_SERVER
to Get a Single HTTP Request Header for Your Server in PHP -
Use the
apache_request_headers()
Function to Get All the Request Headers of Your Apache Server in PHP -
Use
$_SERVER
to Get All HTTP Request Headers for Your Server in PHP
The HTTP headers transfer the data between the web server and browser for communication.
Whenever we type a URL in the browser’s address bar, it sends an HTTP request to the server; it contains a header.
Use the get_headers()
to Get Headers of a Given URL in PHP
The get_headers()
is a PHP built-in function to get headers sent by the server in response to an HTTP request.
<?php
$URL = 'https://www.delftstack.com/';
$headers = get_headers($URL);
foreach($headers as $value) {
echo $value;
echo "<br>";
}
?>
The code above gets all headers sent by the server for https://www.delftstack.com/.
Output:
HTTP/1.0 200 OK
Age: 0
Cache-Control: max-age=2592000, private, s-maxage=0, proxy-revalidate
Content-Type: text/html; charset=UTF-8
Date: Fri, 25 Feb 2022 12:00:31 GMT
Display: pub_site_to_orig_sol
Etag: "6b7e22637c1ca646a2c1db6894a4b0f8-ssl-df-gzip"
Pagespeed: off
Response: 200
Server: nginx
Set-Cookie: ezoadgid_96282=-1; Path=/; Domain=delftstack.com; Expires=Fri, 25 Feb 2022 12:30:31 UTC
Set-Cookie: ezoref_96282=; Path=/; Domain=delftstack.com; Expires=Fri, 25 Feb 2022 14:00:31 UTC
Set-Cookie: ezoab_96282=mod1; Path=/; Domain=delftstack.com; Expires=Fri, 25 Feb 2022 14:00:31 UTC
Set-Cookie: active_template::96282=pub_site.1645790431; Path=/; Domain=delftstack.com; Expires=Sun, 27 Feb 2022 12:00:31 UTC
Set-Cookie: lp_96282=https://www.delftstack.com/; Path=/; Domain=delftstack.com; Expires=Fri, 25 Feb 2022 12:30:31 UTC
Set-Cookie: ezovuuidtime_96282=1645790431; Path=/; Domain=delftstack.com; Expires=Sun, 27 Feb 2022 12:00:31 UTC
Set-Cookie: ezovuuid_96282=23606c54-6e8e-42a1-4745-bf6c0d668d64; Path=/; Domain=delftstack.com; Expires=Fri, 25 Feb 2022 12:30:31 UTC
Set-Cookie: ezopvc_96282=1; Path=/; Domain=delftstack.com; Expires=Fri, 25 Feb 2022 12:30:31 UTC
Strict-Transport-Security: max-age=31536000
Vary: Accept-Encoding
Vary: Accept-Encoding,User-Agent
X-Ezoic-Cdn: Hit ds;mm;ba60b18a465a11ac0d2ea4d2e9f91570;2-96282-33;ec6a48c1-7683-4a48-429a-88731467543c
X-Middleton-Display: pub_site_to_orig_sol
X-Middleton-Response: 200
X-Nf-Request-Id: 01FWR6BN2347S6Y8M9PW8W70CW
X-Origin-Cache-Control: public, max-age=0, must-revalidate
X-Sol: pub_site
Use $_SERVER
to Get a Single HTTP Request Header for Your Server in PHP
Our localhost server contains all header information in the $_SERVER
array. We can get the information for a single HTTP request header by putting the particular index name.
<?php
//print_r($_SERVER);
echo $_SERVER['HTTP_HOST']."<br>";
echo $_SERVER['HTTP_USER_AGENT']."<br>";
echo $_SERVER['HTTP_CONNECTION'];
?>
The code above gets the information of the given single HTTP request.
Output:
localhost
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:97.0) Gecko/20100101 Firefox/97.0
keep-alive
Use the apache_request_headers()
Function to Get All the Request Headers of Your Apache Server in PHP
apache_request_headers()
built-in function in PHP used to get headers of apache module.
<?php
$apache_headers= apache_request_headers();
foreach ($apache_headers as $key => $value) {
echo "$key => $value <br/>";
}
?>
The output will show HTTP information of the apache module:
Host => localhost
User-Agent => Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:97.0) Gecko/20100101 Firefox/97.0
Accept => text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8
Accept-Language => en-US,en;q=0.5
Accept-Encoding => gzip, deflate
Connection => keep-alive
Upgrade-Insecure-Requests => 1
Sec-Fetch-Dest => document
Sec-Fetch-Mode => navigate
Sec-Fetch-Site => none
Sec-Fetch-User => ?1
The getallheaders()
is a newer version of apache_request_headers
and works similarly.
Use $_SERVER
to Get All HTTP Request Headers for Your Server in PHP
$_SERVER
contains a lot of information other than HTTP request headers.
<?php
function get_HTTP_request_headers() {
$HTTP_headers = array();
foreach($_SERVER as $key => $value) {
if (substr($key, 0, 5) <> 'HTTP_') {
continue;
}
$single_header = str_replace(' ', '-', ucwords(str_replace('_', ' ', strtolower(substr($key, 5)))));
$HTTP_headers[$single_header] = $value;
}
return $HTTP_headers;
}
$headers = get_HTTP_request_headers();
foreach ($headers as $key => $value) {
echo "$key => $value <br/>";
}
?>
The above code will extract HTTP request headers from the $_SERVER
array.
Output:
Host => localhost
User-Agent => Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:97.0) Gecko/20100101 Firefox/97.0
Accept => text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8
Accept-Language => en-US,en;q=0.5
Accept-Encoding => gzip, deflate
Connection => keep-alive
Upgrade-Insecure-Requests => 1
Sec-Fetch-Dest => document
Sec-Fetch-Mode => navigate
Sec-Fetch-Site => none
Sec-Fetch-User => ?1
Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.
LinkedIn Facebook