PHP Get header Information implementation steps for HTTP request

Source: Internet
Author: User

PHP how to get HTTP request header information, is an urgent solution and do not know how to choose the problem, I collected and collated, for reference

The PHP manual provides ready-made functions:
Getallheaders
(PHP 4, PHP 5)
Getallheaders-fetch all HTTP Request headers
Description
Array getallheaders (void)
Fetches all HTTP headers from the current request.
This function is a alias for Apache_request_headers (). Please read theapache_request_headers () documentation for more information on how the This function works.
return value
An associative array of all the HTTP headers in the "current request", Orfalse on failure.
Example #1 getallheaders () Example

Copy CodeThe code is as follows:
<?php
foreach (Getallheaders () as $name = = $value) {
echo "$name: $value \ n";
}
?>


However, this function can only be used in Apache environment, IIS or Nginx is not supported, can be implemented by custom functions

Copy CodeThe code is as follows:
<?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, let's see what's printed out.

Copy CodeThe code is as follows:
<?php
Print_r (Getallheaders ());


Get results:

Copy CodeThe code is as follows:


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
)

PHP Get header Information implementation steps for HTTP request

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.