How to read any request header in PHP

藏色散人
Release: 2023-04-04 20:18:02
Original
3661 people have browsed it


HTTP headers: HTTP headers are codes that transfer data between a web server and a browser. HTTP headers are mainly used for two-way communication between server and client.

How to read any request header in PHP

HTTP Request Headers: When you type a URL into the browser's address bar and try to access it, the browser sends an HTTP request to the server. HTTP request headers contain information in the form of text records, which include a lot of useful information, such as the type, capabilities and version of the browser that generated the request, the operating system used by the client, the page requested, the various types accepted by the browser output, etc. After receiving the request headers, the web server sends HTTP response headers back to the client.

Example 1: Reading any request header can be achieved using the getallheaders() function.

<?php 
foreach (getallheaders() as $name => $value) { 
    echo "$name: $value <br>"; 
} 
?>
Copy after login

The output is as follows:

Host: 127.0.0.3:2025 
Connection: keep-alive 
Cache-Control: max-age=0 
Upgrade-Insecure-Requests: 1 
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, 
like Gecko) Chrome/70.0.3538.67 Safari/537.36 
Accept: text/html, application/xhtml+xml, application/xml;q=0.9, 
image/webp, image/apng, */*;q=0.8 
Accept-Encoding: gzip, deflate, br 
Accept-Language: en-US, en;q=0.9
Copy after login

Note: getallheaders - Get all HTTP request header information, that is, get all request header information for the current request. This function is an alias of apache_request_headers().

Example 2: It can also be implemented using the apache_request_headers() function.

<?php 
$header = apache_request_headers(); 
  
foreach ($header as $headers => $value) { 
    echo "$headers: $value <br />\n"; 
} 
?>
Copy after login

Output:

Host: 127.0.0.6:2027 
Connection: keep-alive 
Cache-Control: max-age=0 
Upgrade-Insecure-Requests: 1 
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML,
like Gecko) Chrome/70.0.3538.67 Safari/537.36 
Accept: text/html, application/xhtml+xml, application/xml;q=0.9, 
image/webp, image/apng, */*;q=0.8 
Accept-Encoding: gzip, deflate, br 
Accept-Language: en-US, en;q=0.9
Copy after login

Note: apache_request_headers - Get all HTTP request header information

This article is about reading any request header in PHP The method is introduced, simple and easy to understand, I hope it will be helpful to friends in need!


The above is the detailed content of How to read any request header in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template