Please wait

HTTP Headers

HTTP headers are a component of the HTTP protocol, which is used for communication on the web. Headers provide important metadata about the HTTP request or response, such as the type of content, the status of the response, cookies, and more. Each HTTP header consists of a name and a value, separated by a colon.

In PHP, you can use the header() function to send raw HTTP headers. You can use it to set new headers, modify existing headers, or even send HTTP status codes.

Here is the syntax for the header() function:

header(string $header, bool $replace = true, int $response_code = 0): void
  • $header - Required. It specifies the header string to send. For example, to tell the browser that you're sending HTML content, you would use: header("Content-Type: text/html");.
  • $replace - Optional. It indicates whether the header should replace a previous similar header. true (it will replace), false (it will not). Default is true. If it is set to false, it will create additional headers of the same type.
  • $response_code - Optional. It forces the HTTP response code to the specified value (PHP 5.1.2 and newer).

Redirection

One of the common use cases for using the header() function is to redirect a user. To redirect a user to another URL, you can send a Location header with the URL of the new page. Here's an example:

header("Location: https://www.example.com");

In this case, the browser will receive the Location header and automatically redirect the user to the URL specified (https://www.example.com).

Content Type

Another use case is to change the content type. The Content-Type is an HTTP header that indicates the media type of the resource or the data being sent to the client. In other words, it tells the client what the content is composed of and how to interpret it.

Here are some common examples of Content-Type values:

  • text/html: HTML document (default for many web servers)
  • text/css: CSS file
  • text/javascript: JavaScript file
  • application/json: JSON data
  • image/jpeg: JPEG image file
  • audio/mpeg: MPEG audio file
  • video/mp4: MP4 video file
  • application/pdf: PDF document
  • multipart/form-data: Used when a form is submitted and it contains a file, non-ASCII data, or binary data

For example, if a server sends a response with Content-Type: application/json, it means that the server is sending JSON data, and the client should interpret it as such. Similarly, a Content-Type: image/jpeg means that the server is sending a JPEG image.

The Content-Type value is important as it helps the client (often a web browser) to understand how to handle the received data correctly. Without it, the client might not be able to display the data to the user in the correct format.

To change the content type of the HTTP response, you can send a Content-Type header with the desired MIME type. For example, if you want to send JSON data, you can do:

header("Content-Type: application/json");

In this case, you're telling the client that you're sending JSON data. This is important because it allows the client to correctly interpret the data that it receives.

Key Takeaways

  • HTTP headers provide metadata about HTTP requests and responses, such as the type of content, the status of the response, and more.
  • In PHP, the header() function is used to send raw HTTP headers.
  • To redirect a user to another URL, you can send a Location header with the URL of the destination page.
  • To change the content type of the response, you can send a Content-Type header with the desired MIME type.

Comments

Please read this before commenting