PHP getallheaders cannot get the custom headers.
A custom http header is added to the client request. The request is as follows:
Custom http Request Header
var_dump(getallheaders);
At first, the getallheaders parameter was obtained, but it was found that it could not be obtained on the nginx deployed server. It was very strange that the getallheaders function only supported the apache server. Then find the 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());
In fact, this method finds the attribute starting with HTTP _ in the $ _ SERVER variable and replaces the attribute with a string. In the $ _ SERVER variable, HTTP_USER_ID is actually the User-Id defined above:
$ _ SERVER variable in php
In addition, for custom Http headers, note the naming rules of headers. Do not use underscores to name them. Otherwise, the headers cannot be read on the nginx server. When searching for naming rules, we have mentioned that custom attributes start with X. Later, I checked some information and found that later http protocol is not recommended.
The above is a description of the problem that PHP getallheaders cannot obtain the custom header (headers). I hope it will help you!
Articles you may be interested in:
- Php session_start () about Cannot send session cache limiter-headers already sent error Solution
- Sample Code for simulating the get_headers function in php
- Use php get_headers to determine whether the URL is valid
- The role and usage of the get_headers function in php
- PHP prompts Cannot modify header information-headers already sent by Solution
- PHP error Warning: Cannot modify header information-headers already sent by Solution
- PHP uses the get_headers function to determine whether a remote file exists.
- The get_headers function with timeout implemented by PHP