adds a custom http header when the client requests. The request is as follows:
Custom http request header
var_dump(getallheaders);
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());
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!