Getting headers in PHP
HTTP headers carry data between web servers and browsers for communication.
Whenever we enter a URL in the address bar of our browser, it sends an HTTP request to the server; it contains a header.
In PHP use get_headers()
get the headers for a given URL
get_headers()
Is a PHP built-in function that is used to get the 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 above code gets all the 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
$_SERVER
Get a single HTTP request header for your server in PHP using
Our localhost server contains $_SERVER
all the header information in an array. We can get the information of a single HTTP request header by putting a specific index name.
<?php
//print_r($_SERVER);
echo $_SERVER['HTTP_HOST']."<br>";
echo $_SERVER['HTTP_USER_AGENT']."<br>";
echo $_SERVER['HTTP_CONNECTION'];
?>
The above code gets information given a 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 function in PHP apache_request_headers()
to get all request headers of the Apache server
The built-in function in PHP apache_request_headers()
is used to get the headers of the apache module.
<?php
$apache_headers= apache_request_headers();
foreach ($apache_headers as $key => $value) {
echo "$key => $value <br/>";
}
?>
The output will show the HTTP information for 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
getallheaders()
is apache_request_headers
an updated version of and works in a similar way.
$_SERVER
Get all HTTP request headers from the server using
$_SERVER
Contains a lot of information other than the HTTP request header.
<?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 $_SERVER
extract the HTTP request headers from the 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
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Check if a Post exists in PHP
Publish Date:2025/04/13 Views:170 Category:PHP
-
PHP $_POST is a super global variable that can contain key-value pairs of HTML form data submitted through the post method. We will learn different ways to check $_POST if a and contains some data in this article. These methods will use iss
PHP with Ajax
Publish Date:2025/04/13 Views:139 Category:PHP
-
We will use PHP and ajax by printing a simple sum of two numbers 2 and . Also, print a php array in JSON. 3 object We will also use PHP with ajax by getting the HTML formatted output from the number division in PHP. Printing simple addition
Store Div Id in PHP variable and pass it to JavaScript
Publish Date:2025/04/13 Views:51 Category:PHP
-
This article shows you how to div id store a in a PHP variable and pass it to JavaScript code. We will answer the following questions. What is div id ? How to div id store in a PHP variable? How to pass variables to JavaScript code? Let’s
Returns the article tag with ID from the action page
Publish Date:2025/04/13 Views:80 Category:PHP
-
Let's say you're in a login form and you enter the wrong information; in this case, you probably want to go back to the login page. PHP has a built-in function header() to redirect a page to a specific page. But what if the login page is at
Switching PHP versions on Ubuntu
Publish Date:2025/04/13 Views:78 Category:PHP
-
Different tasks may require running multiple versions of PHP. You may need to switch PHP versions by running two sites on the same server or testing older versions of code using outdated methods. We can switch PHP versions on Ubuntu using t
Resizing images in PHP
Publish Date:2025/04/13 Views:155 Category:PHP
-
In this tutorial article, we will discuss about resizing images in PHP. Load the image before resizing Before we can resize an image, we must first load it as an image resource in our script. This is file_get_contents() different from using
PHP upload image
Publish Date:2025/04/13 Views:61 Category:PHP
-
We can upload images in PHP using simple file upload operation, but first, php.ini file upload should be enabled from Files. This tutorial demonstrates how to upload images in PHP. php.ini Enable file upload from file in PHP to upload image
Creating a signature from Hash_hmac() and Sha256 in PHP
Publish Date:2025/04/13 Views:107 Category:PHP
-
PHP has one of the best encryption functions for data security. Hash_hmac() The encrypt function is one of the most famous encryptors. We'll show you how to use hash_hmac and sha256 encryptors to create 安全签名 one that you can store i
Updating PHP 7.x to 7.4 on CentOS
Publish Date:2025/04/13 Views:131 Category:PHP
-
This article shows the steps to update the PHP version from 7.x version to 7.4 in CentOS. How to Update PHP from 7.X to 7.4 in CentOS Update operating system packages. yum update -y Check your PHP version in CentOS. php -v Prints a list of