Home >Backend Development >PHP Tutorial >Introduction to PHP functions: header() function

Introduction to PHP functions: header() function

王林
王林Original
2023-11-04 11:25:532158browse

Introduction to PHP functions: header() function

Introduction to PHP functions: header() function, which implements web page jumps and sets HTTP response headers

In PHP, the header() function is a very important function , it can not only jump to web pages, but also set HTTP response header information. This article will introduce the use of header() function in detail and provide specific code examples.

The basic syntax of the header() function is as follows:

header(string $header, bool $replace = true, int $http_response_code = 0): bool
  • $header (required): The HTTP header to be sent. String form, for example: "Content-Type: text/html;charset=utf-8".
  • $replace (optional): Specify whether to replace the previous header with the same name. The default is true, which means replacement; false, which means no replacement.
  • $http_response_code (optional): Set the HTTP response status code. Must be a valid HTTP status code.

The following are common application scenarios and specific code examples of the header() function:

  1. Realizing web page jumps
    The header() function can redirect users Go to the specified URL to realize the function of web page jump. For example, redirect the user to another page:
header("Location: http://www.example.com");
exit;
  1. Set HTTP response header
    The header() function can also be used to set HTTP response header information, such as setting Content-Type , Content-Disposition, etc. For example, set Content-Type to JSON format:
header("Content-Type: application/json");
  1. Set HTTP response status code
    The header() function can also set the status code of HTTP response, such as setting 200 to indicate success, 404 means the page does not exist, etc. For example, set the 404 page does not exist status code:
header("HTTP/1.1 404 Not Found");
  1. Prevent page caching
    Another common use of the header() function is to prevent the page from being cached. By setting Cache-Control to no-cache, you can tell the browser not to cache the page. For example:
header("Cache-Control: no-cache, no-store, must-revalidate");
header("Pragma: no-cache");
header("Expires: 0");
  1. Set file download
    By setting Content-Disposition to attachment, the file download function can be realized. For example, download a file named example.pdf:
header("Content-Disposition: attachment; filename=example.pdf");
header("Content-Type: application/pdf");
header("Content-Length: " . filesize("example.pdf"));
readfile("example.pdf");

Summary: The
header() function is a very important PHP function, which can realize web page jumps and set HTTP response headers and other functions . Its flexibility allows us to flexibly adjust HTTP header information according to needs. We should be familiar with how to use the header() function and use it reasonably to achieve the functions we need.

Please note that the header() function must be called before all output, otherwise an error will be reported. After calling the header() function, in order to avoid unexpected situations, we recommend using exit to terminate script execution immediately.

I hope that through the introduction of this article, readers can fully understand the usage of the header() function and be able to use it flexibly in actual projects.

The above is the detailed content of Introduction to PHP functions: header() function. For more information, please follow other related articles on the PHP Chinese website!

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