In PHP, the header() function is used to send raw HTTP header information to the browser. It is usually used for redirection, setting content type, controlling cache, setting HTTP status codes and other scenarios. In many cases, the default HTTP status code (such as 200) is sufficient, but under certain special needs, we may need to customize the HTTP status code. This article will introduce how to use the header() function to correctly set custom HTTP status codes.
The HTTP status code is a three-digit code sent by the server when responding to a client request, which represents the result of the request. Common status codes are:
200 OK: The request was successful
301 Moved Permanently: Permanently Redirected
404 Not Found: Page Not Found
500 Internal Server Error: Internal Server Error
You can customize the status code according to your needs to describe different requests and responses more accurately.
The basic syntax of the header() function is:
header(string $header, bool $replace = true, int $http_response_code = 0)
$header : The parameter that must be passed to specify the content of the HTTP header information. You can set content type, redirection, status code, etc.
$replace : This is an optional parameter, default to true , indicating whether to replace the existing header information.
$http_response_code : This is an optional parameter to specify the HTTP status code. If the parameter is 0, header() will automatically set the status code according to the header value you provide.
To set a custom HTTP status code, just specify HTTP/1.1 in the header() function followed by the custom status code and description information. Examples are as follows:
<?php
// set up 404 Not Found Status code
header("HTTP/1.1 404 Not Found");
?>
If you need to return a specific status code and description for a request, you can also use codes like the following:
<?php
// set up 403 Forbidden Status code
header("HTTP/1.1 403 Forbidden");
?>
You can set any standard HTTP status code in a similar way, just change the status code and description to what you need.
Suppose you are developing a website and want to return a custom 404 error page when the user visits a page that does not exist, you can use the following code:
<?php
// set up 404 Not Found Status code
header("HTTP/1.1 404 Not Found");
// Will URL Error page in(For example, 'error.php')Show to the user
include('error.php');
?>
In this example, when the user accesses a page that does not exist, the server returns a 404 status code and displays the error.php page.
If you want to redirect the user to another URL immediately after the page is accessed, you can use the following code:
<?php
// set up 301 永久重定向Status code
header("HTTP/1.1 301 Moved Permanently");
// set up新的重定向 URL
header("Location: https://m66.net/newpage");
exit;
?>
This code redirects the user to https://m66.net/newpage and tells the browser that this is a permanent redirect.
When you encounter an internal server error, you can set the 500 status code and return a custom error page, like this:
<?php
// set up 500 Internal Server Error Status code
header("HTTP/1.1 500 Internal Server Error");
// Return to the custom error page
include('500_error.php');
?>
In this example, the 500_error.php page is displayed to the user, informing them that a server error has occurred.
The header() function must be called before any output, otherwise the HTTP header information will not be modified. If you have any HTML output or echo output before calling the header() function, it will cause an error.
Each page can only send HTTP header information once. Pay special attention when calling the header() function multiple times and do not repeatedly set the same status code or header information.
If you want to redirect, remember to add exit after header() , otherwise the program may continue to execute subsequent code, resulting in unnecessary behavior.
The header() function is a very powerful tool in PHP, which can be used to set HTTP status codes, perform page redirection, control cache, etc. By using the header() function rationally, we can better control the user experience and browser behavior. During development, when you need to return a specific status code to the user, remember to correctly set the HTTP header information to ensure the stability and correctness of the website.