php obtains client and server related information

WBOY
Release: 2016-07-25 09:07:22
Original
855 people have browsed it
  1. $headers = array();
  2. foreach ($_SERVER as $key => $value) {
  3. if ('HTTP_' == substr($key, 0, 5)) {
  4. $headers[str_replace('_', '-', substr($key, 5))] = $value;
  5. }
  6. }
  7. ?>
Copy code

Explanation: Clear in RFC Pointed out that header names are not case-sensitive.

However, not all HTTP request headers exist in $_SERVER in the form of keys starting with HTTP_. For example, Authorization, Content-Length, and Content-Type are not like this, so in order to obtain all HTTP request headers , you also need to add the following code:

  1. if (isset($_SERVER['PHP_AUTH_DIGEST'])) {
  2. $header['AUTHORIZATION'] = $_SERVER['PHP_AUTH_DIGEST']);
  3. } elseif (isset($ _SERVER['PHP_AUTH_USER']) && isset($_SERVER['PHP_AUTH_PW'])) {
  4. $header['AUTHORIZATION'] = base64_encode($_SERVER['PHP_AUTH_USER'] . ':' . $_SERVER['PHP_AUTH_PW'] ));
  5. }
  6. if (isset($_SERVER['CONTENT_LENGTH'])) {
  7. $header['CONTENT-LENGTH'] = $_SERVER['CONTENT_LENGTH'];
  8. }
  9. if (isset($_SERVER[' CONTENT_TYPE'])) {
  10. $header['CONTENT-TYPE'] = $_SERVER['CONTENT_TYPE'];
  11. }
  12. ?>
Copy code


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