PHP getallheaders cannot get the custom headers.

Source: Internet
Author: User
Tags nginx server

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

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.