When a browser accesses a web page, the server first establishes communication with the browser through the HTTP protocol. This process involves sending HTTP header information and subsequent HTML content. HTTP header information not only contains basic information of the web page (such as content type, character set, etc.), but also determines how the browser handles the page display, cache, jump and other behaviors.
In PHP, we use the header() function to send HTTP headers. For example, the following code will redirect the browser to a new page:
<?php
header("Location: http://m66.net/newpage.php");
exit;
?>
When this code is executed, the server will send a redirect header ( Location ), and the browser will jump based on this header information. At this time, the PHP script will stop executing (due to exit() ), so subsequent HTML content will not be output.
In PHP, the HTTP header must be sent before any HTML output. This is because the HTTP header information is sent first by the server, and the HTML content is subsequent data. If you first output HTML content or any other characters in PHP, and then call the header() function, PHP will report an error, prompting "headers already sent".
For example, the following code will error:
<?php
echo "Hello World!"; // Output HTML content
header("Location: http://m66.net/newpage.php"); // Try sending a redirect header
exit;
?>
When this code is executed, a warning will be thrown, prompting that the header has been sent. The reason is that the echo statement has outputted HTML content, which causes the HTTP header to be no longer sent.
In the HTTP protocol, header information must be sent before the actual page content (HTML, pictures, CSS, etc.). The browser decides how to handle the received page based on the received header information. For example, the Location header indicates that the browser should redirect, while Content-Type tells the browser the content type of the current page.
PHP needs to send these headers through the header() function before outputting HTML content, because once HTML starts to be sent to the browser, the server can no longer change the HTTP header. To ensure that the HTTP header can be successfully modified (such as redirecting), you must make sure that header() is called before any output.
Although PHP requires that header() be called before outputting any HTML content, in some cases, Output Buffering can help solve this problem. Output buffering allows PHP to store HTML content in memory before actually sending it, so that it can be unaffected by the output content when subsequent calls to header() .
Examples of code using output buffering are as follows:
<?php
ob_start(); // Turn on output buffering
echo "Hello World!"; // 仍然可以输出content
header("Location: http://m66.net/newpage.php"); // Send header
exit;
?>
In the above code, the ob_start() function enables output buffering, so that even if the content is output after echo , PHP does not send the content to the browser immediately, but stores them in the buffer. This allows you to subsequently call the header() function to modify the HTTP header without encountering the "headers already sent" error.
Page redirection: Use header("Location: URL") to redirect the user to another page.
header("Location: http://m66.net/anotherpage.php");
exit;
Set content type: Use header("Content-Type: type") to set the content type of the page, for example:
header("Content-Type: application/json");
echo json_encode($data);
Control cache: Use header() to set cache control to indicate whether the browser should cache a certain page.
header("Cache-Control: no-cache, no-store, must-revalidate");
header("Pragma: no-cache");
header("Expires: 0");
PHP's header() function is used to send HTTP header information, and its call order is crucial to the output order of the page. Since the HTTP header must be sent before the HTML content, you must make sure that nothing is output when calling header() . To solve this problem, output buffering can be used as an effective solution. Understanding the relationship between the header() function and the page output order will help avoid errors and unexpected behaviors caused by incorrect call order.
By using the header() function and output buffering rationally, you can ensure that the PHP scripts work as expected and avoid unnecessary errors.