Home > Backend Development > PHP Tutorial > PHP getallheaders cannot get custom headers (headers), phpgetallheaders_PHP tutorial

PHP getallheaders cannot get custom headers (headers), phpgetallheaders_PHP tutorial

WBOY
Release: 2016-07-12 08:56:16
Original
1312 people have browsed it

PHP getallheaders cannot obtain custom headers (headers). phpgetallheaders

adds a custom http header when the client requests. The request is as follows:

Custom http request header

var_dump(getallheaders);
Copy after login

At first, it was obtained through the getallheaders parameter, but it was found that it could not be obtained on the server deployed by nginx. It was very strange. After checking the PHP manual, I found that the getallheaders function only supports the apache server. So I found a compatible method:

if (!function_exists('getallheaders')) {
function getallheaders() {
$headers = array();
foreach ($_SERVER as $name => $value) {
if (substr($name, 0, 5) == 'HTTP_') {
$headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
}
}
return $headers;
}
}
var_dump(getallheaders());
Copy after login

In fact, this method is to find the attributes starting with HTTP_ in the $_SERVER variable, and do a string replacement for the attributes like this. The HTTP_USER_ID in the $_SERVER variable is actually the customized User-Id above:

$_SERVER variable in php

In addition, regarding custom Http headers, you need to pay attention to the naming convention of the header. You cannot name it with underscores, otherwise it will not be read under the nginx server. When looking for the naming convention, it is mentioned that custom attributes start with X- question. Later, I checked some information and found that the later http protocol did not recommend doing this.

The above content is a description of the problem that PHP getallheaders cannot obtain custom headers. I hope it will be helpful to everyone!

Articles you may be interested in:

  • php session_start() About Cannot send session cache limiter - headers already sent error solution
  • php code example to simulate get_headers function
  • The solution to use php get_headers to determine whether the URL is valid
  • A detailed introduction to the role and usage of the get_headers function in php
  • PHP prompts Cannot modify header information - headers already sent bySolution Method
  • PHP error Warning: Cannot modify header information - headers already sent bySolution
  • PHP uses the get_headers function to determine whether the remote file exists
  • PHP implemented with timeout Function get_headers function

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1113707.htmlTechArticlePHP getallheaders cannot obtain custom headers (headers). phpgetallheaders adds customization when the client requests The http header, the request is as follows: Custom http request header...
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