What is a request header? What are the common request headers?

Table of contents

1. What is a request header?

2. What are the common request headers?

3. Common http headers?


1. What is a request header?

Request Headers are parts of the HTTP protocol used to convey additional information about the request. It contains the metadata required for communication between the client (usually a browser or application) and the server.

The functions of request headers include the following aspects:

  1. Pass additional information of the request: The request header can carry some client-related information, such as user agent (User-Agent), accepted content type (Accept), authentication credentials (Authorization), etc. This information helps the server better understand and handle requests.

  2. Control caching behavior: Through the Cache-Control field in the request header, the client can tell the server how to handle response caching, including whether to use caching, the cache validity period, etc.

  3. Authentication: The Authorization field in the request header is often used to pass authentication credentials, such as Basic Authentication or Token. The server can authenticate the request against these credentials to determine whether access to the protected resource is allowed.

  4. Control the format of the request body: The Content-Type field specifies the format type of the data in the request body, such as JSON, form data, etc. The server can correctly parse the data in the request body based on Content-Type.

  5. Provide jump source information: The Referer field indicates which URL page the current request is initiated from, which can help the server identify the source of the request.

Through request headers, the client can provide more information to the server so that the server can process and respond accordingly based on this information. At the same time, the server can also use request headers to control and manage the behavior of requests to ensure smooth communication.

2. What are the common request headers?

In PHP, common request headers include the following:

  1. User-Agent: Identifies the browser and operating system information used by the client. Can be $_SERVER['HTTP_USER_AGENT']obtained through .

  2. Accept: Specifies the content types that the client can handle, that is, acceptable media types. Can be $_SERVER['HTTP_ACCEPT']obtained through .

  3. Content-Type: Specifies the data format type in the request body. Common values ​​include application/json, application/x-www-form-urlencodedetc. Can be $_SERVER['CONTENT_TYPE']obtained through .

  4. Authorization: Credential information used for authentication. Common values ​​include Bearer Token, Basic Authentication, etc. Can be $_SERVER['HTTP_AUTHORIZATION']obtained through .

  5. Cookie: Contains cookie information from the client. Can be $_SERVER['HTTP_COOKIE']obtained through .

  6. Referer: Indicates which URL page the current request is initiated from. Can be $_SERVER['HTTP_REFERER']obtained through .

  7. Host: Specify the domain name or IP address of the server. Can be $_SERVER['HTTP_HOST']obtained through .

  8. X-Requested-With: Indicates whether the request is initiated by Ajax. This header field is usually set in Ajax requests, and its value is "XMLHttpRequest". Can be $_SERVER['HTTP_X_REQUESTED_WITH']obtained through .

  9. Content-Length: Specifies the length of the request body. Can be $_SERVER['CONTENT_LENGTH']obtained through .

  10. Cache-Control: Directives that control cache behavior. Used to specify how clients and proxy servers cache responses. Can be $_SERVER['HTTP_CACHE_CONTROL']obtained through .

These are common request headers in PHP, and $_SERVERtheir values ​​can be obtained using superglobal variables. Depending on the specific needs, you can process these request headers in PHP through corresponding methods or variables.

3. Common http headers?

In PHP, common HTTP headers (HTTP Headers) can header()be set through functions. Here are some common HTTP headers and their uses:

Content-Type: Specifies the response content type returned by the server. Common values ​​include  text/html, application/json, image/jpeg etc.

header('Content-Type: text/html; charset=utf-8');

Location: used to redirect to other pages. The header can be set when the server needs to redirect the client to another page  Location . 

header('Location: http://example.com/redirected-page');

Cache-Control: Sets a cache control policy that instructs the client or intermediate proxy how to cache responses. Common values ​​include  no-cache, max-age etc.

header('Cache-Control: no-cache, max-age=0');

Expires: Set the expiration time of the response. Specifies a date and time after which clients will no longer use the cached copy.

$expires = gmdate('D, d M Y H:i:s', time() + 3600) . ' GMT'; header('Expires: ' . $expires);

 Set-Cookie: Set the Cookie value to be stored on the client side. You can use this header to set session identifiers, tracking information, etc. for the client.

setcookie('name', 'value', time() + 3600, '/'); header('Set-Cookie: name=value; expires=Wed, 01-Sep-2023 00:00:00 GMT; path=/');

php copy code Access-Control-Allow-Origin: used for cross-domain requests to specify the source that is allowed to access resources. Can be set to a specific domain name or  *(meaning allowing any origin).

header('Access-Control-Allow-Origin: https://example.com');

Content-Disposition: Instructs the client how to handle the response body returned by the server. A common use is to set the response to downloading a file.

header('Content-Disposition: attachment; filename="file.txt"');

Guess you like

Origin blog.csdn.net/smallmww/article/details/132509151