php
  1. php-header

PHP header() Function

The header() function is a built-in function in PHP that sends a raw HTTP header to the client. It is used to redirect the user to a different location and to send additional HTTP headers.

Syntax

The basic syntax of the header() function in PHP is:

header(string $header, bool $replace = true, int $http_response_code = null)
  • $header: The mandatory argument that specifies the header string to send.
  • $replace: An optional argument that specifies whether the header should be replaced or appended to the existing headers. The default value is true.
  • $http_response_code: An optional argument that specifies the HTTP response code to send. This value should be a valid HTTP response code. If not specified, the default value is 200 OK.

Example

The following code example shows how to use the header() function to redirect the user to another page:

<?php
header("Location: https://www.example.com");
exit;
?>

Output

When the header() function is executed, it sends the specified header to the client. In the example above, the user is redirected to the URL "https://www.example.com".

Explanation

The header() function is typically used in conjunction with the Location header to redirect users to a different page. In the example above, the header() function is used to send the Location header, which specifies the URL of the page to which the user should be redirected.

The exit statement is used to stop the execution of the script immediately after the header() function is called. This is necessary because if the script continues to execute, additional output may be sent to the client, which could interfere with the redirection.

The header() function can also be used to send other HTTP headers, such as Content-Type, Expires, and Cache-Control, among others.

Use

The header() function is commonly used to perform HTTP redirections and to send additional headers that control caching and other aspects of the HTTP response.

Some common use cases for the header() function include:

  • Redirecting users to a different page
  • Setting the content type of the HTTP response
  • Controlling caching behavior of the response
  • Specifying the last modification date of a page

Important Points

When using the header() function, there are some important points to keep in mind:

  • The header() function must be called before any output is sent to the client. This is because HTTP headers must be sent before any actual content.
  • The header() function can only be called once per page request. Attempting to call it multiple times will result in an error.
  • The header() function should only be used to send legitimate HTTP headers. Attempting to send malformed or invalid headers can result in unexpected behavior or errors.

Summary

The header() function is a powerful and flexible function in PHP that allows developers to send raw HTTP headers to the client. It is commonly used for performing redirects and controlling the caching behavior of pages, among other use cases. However, it should be used with caution and only to send legitimate headers to avoid errors and unexpected behavior.

Published on: