Home  >  Article  >  Backend Development  >  php obtains client and server related information

php obtains client and server related information

WBOY
WBOYOriginal
2016-07-25 09:07:22788browse
  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


Statement:
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