Home > Backend Development > PHP Tutorial > How to get the header information of http request in php

How to get the header information of http request in php

WBOY
Release: 2016-07-25 09:07:24
Original
909 people have browsed it
  1. foreach (getallheaders() as $name => $value) {
  2. echo "$name: $valuen";
  3. }
  4. ?>
Copy code

However, this function can only be used in the apache environment. It is not supported by iis or nginx. It can be implemented through a custom function.

  1. if (!function_exists('getallheaders'))
  2. {
  3. function getallheaders()
  4. {
  5. foreach ($_SERVER as $name => ; $value)
  6. {
  7. if (substr($name, 0, 5) == 'HTTP_')
  8. {
  9. $headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5))))] = $value;
  10. }
  11. }
  12. return $headers;
  13. }
  14. }
  15. ?>
Copy the code

and check it Output the results.

  1. print_r(getallheaders());
  2. ?>
Copy the code

to get the result: 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 )



source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template