Home >Backend Development >PHP Tutorial >How to read HTTP request headers in PHP

How to read HTTP request headers in PHP

青灯夜游
青灯夜游Original
2019-01-05 11:59:188281browse

If you want to read request headers in PHP, you can use the getallheaders() function and the apache_request_headers() function. The following article will introduce you to the method of reading request headers in PHP. I hope it will be helpful to you.

How to read HTTP request headers in PHP

What are the HTTP request headers?

Before understanding the HTTP request headers, let’s first take a look at the HTTP headers.

HTTP header: HTTP header is the code that transmits data between the web server and the browser. It is mainly used for two-way communication between the server and the client.

HTTP request headers:

When you type a URL into your browser's address bar and try to access it, your 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 browser Various types of output are accepted, etc.

After receiving the request headers, the web server sends HTTP response headers back to the client.

php reads request headers

1. Use the getallheaders() function

getallheaders The () function can obtain all the HTTP request header information of the current request; then return an array containing all the header information of the current request. If the acquisition fails, it will return FALSE.

Below we use an example to introduce how the getallheaders() function reads request headers.

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

Output:

2. Use the apache_request_headers() function

apache_request_headers() function You can obtain all HTTP request header information for the current request.

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

Output:

The above is the entire content of this article, I hope it will be helpful to everyone's learning. For more exciting content, you can pay attention to the relevant tutorial columns of the PHP Chinese website! ! !

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

Statement:
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