Use PHP to get all HTTP request headers

Source: Internet
Author: User

Reference website:

Http://www.neatstudio.com/show-377-1.shtml

PHP generally uses getallheaders to get the head, but in fact, some modes are not acquired (previously did not notice in fastcgi This function is not available, of course, I do not test now.) That's what Lao Wang said.

He said:

In PHP, to get all the HTTP request headers, you can use the Getallheaders method, but this method does not exist in any environment, for example, you use fastcgi way to run PHP, there is no such method, so we need to consider other methods, fortunately There's something we want in $_server, and it's the HTTP request header that starts with the key name HTTP_:

$headers = array(); 
foreach ($_SERVER as $key => $value) { 
    if (‘HTTP_‘ == substr($key, 0, 5)) { 
        $headers[str_replace(‘_‘, ‘-‘, substr($key, 5))] = $value; 
    } 
}


The code is simple, and it is important to note that the name of the header is not case-sensitive in the RfC.

Use PHP to get all HTTP request headers

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.