PHP gets HTTP header information and methods to get HTTP header information in CI

Source: Internet
Author: User
Tags vars

How to get HTTP header information in CI:

$this->input->request_headers ()

is useful in non-Apache environments that do not support apache_request_headers (). Returns an array of header (header) requests.

$headers = $this->input->request_headers();

----------------------------------------------------------------------------------------------

Gets the header information for the HTTP request.

The PHP manual provides ready-made functions:

Getallheaders

(PHP 4, PHP 5)

getallheaders- Fetch All HTTP request Headers

Descriptionarray 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 in how this function works.

return value

An associative array of any the HTTP headers in the current request, orFALSE on failure.

Example #1 getallheaders () Example

[PHP]View Plaincopy
    1. <?php
    2. foreach (getallheaders () as $name = = $value) {
    3. echo "$name: $value \ n";
    4. }
    5. ?>

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

[PHP]View Plaincopy
  1. <?php
  2. <span class="html" >if (!function_exists (' getallheaders '))
  3. {
  4. function getallheaders ()
  5. {
  6. foreach ($_server as $name = $value)
  7. {
  8. if (substr ($name, 0, 5) = = ' http_ ')
  9. {
  10. $headers [str_replace (', ' -'), Ucwords (strtolower (str_replace (' _ '), ',  substr ($name, 5))))] = $value;
  11. }
  12. }
  13. return $headers;
  14. }
  15. }</span>
  16. ?>


All right, let's see what's printed out.

[PHP]View Plaincopy
    1. <?php
    2. Print_r (getallheaders ());


Get results:

[HTML]View Plaincopy
      1. ARRAY&NBSP;&NBSP;
      2. (  
      3. > */*  
      4.      [accept-language] => zh-cn  
      5. > gzip, deflate  
      6.     [user-agent] => mozilla/4.0  ( compatible; msie 7.0; windows nt 5.1; trident/4.0; .net clr  2.0.50727)   
      7.     [host] =>  localhost  
      8.     [connection] =>  keep-alive  

PHP gets HTTP header information and how to get HTTP header information in CI

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.