PHP Getallheaders Unable to get custom header (headers) Problem _php instance

Source: Internet
Author: User
Tags nginx server

The custom HTTP headers were added at the time of the client request, and the request looks like this:

Custom HTTP request Headers

Var_dump (getallheaders);

It started with the getallheaders parameter, but found that it was not available on the Nginx deployed server, and it was very strange to see the PHP manual found that getallheaders this function only supports Apache servers. Then find 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 ('], '-', UC Words (Strtolower (Str_replace (' _ ', ', substr ($name, 5)))] = $value;
}
}
return $headers;
}
Var_dump (Getallheaders ());

In fact, this approach is to find the $_server variable with the HTTP_ beginning of the attribute, a string replacement for the property. The http_user_id in the $_server variable is actually the custom User-id above:

$_server Variables in PHP

In addition to the custom HTTP header, you need to pay attention to the naming specification of the header, you cannot use the underscore name, otherwise it is not read under the Nginx server, and when you look up the naming specification, there is a reference to the custom attribute with the X-start problem. Later, I looked up some information and found that the HTTP protocol was not recommended.

The above content is for PHP getallheaders can not get a custom header (headers) related to the issue of the narrative, I hope to help!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.