PHP gets HTTP headers: PHP gets HTTP header information

Source: Internet
Author: User
Tags foreach array header http request return

Gets the header information for the HTTP request.
The PHP manual provides a ready-made function:
Getallheaders
(PHP 4, PHP 5)
Getallheaders-fetch all HTTP Request headers
Description
Arraygetallheaders (void)
Fetches all HTTP headers from the current request.
This function is a alias forapache_request_headers (). Please read the theapache_request_headers () documentation for more information on the How this function works.
return value
An associative array of "all" HTTP headers in the current request, Orfalseon failure.
Example #1getallheaders () Example
<?php
foreach (Getallheaders () as $name => $value) {
echo "$name: $value \ n";
}
?>
However, this function can only be used in the Apache environment, which is not supported by IIS or Nginx, and can be implemented by a custom function
<?php
<span class= "HTML" >if (!function_exists (' getallheaders '))
{
function Getallheaders ()
{
foreach ($_server as $name => $value)
{
if (substr ($name, 0, 5) = = ' Http_ ')
{
$headers [Str_replace (', '-', Ucwords (Strtolower) (Str_replace (' _ ', ', substr ($name, 5)))] = $value;
}
}
return $headers;
}
}</span>
?>
All right, look what's printed out.
<?php
Print_r (Getallheaders ());
Get results
Array
(
[Accept] => */*
[Accept-language] => ZH-CN
[accept-encoding] => gzip, deflate
[User-agent] => mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; trident/4.0. NET CLR 2.0.50727)
[Host] => localhost
[Connection] => keep-alive
) This article links http://www.cxybl.com/html/wlbc/Php/20130326/37406.html

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.