In PHP, the header() function is a very important tool for sending raw HTTP message headers. It allows us to finely control the information returned by the browser in the program, including setting status codes, redirecting, specifying content types, etc. Today, we will focus on how to correctly use the header() function to set some common HTTP status codes: 404 (Not Found), 403 (Forbidden), and 500 (Internal Server Error). These three status codes represent different error statuses. For website developers, it is very important to understand and use these status codes reasonably.
The HTTP status code is a three-digit code returned by the server when responding to the browser, representing the status of the response. Each status code has a different meaning, and they are divided into the following categories:
1xx (informative): means that the request has been accepted and needs to be processed.
2xx (success): means that the request has been successfully processed.
3xx (redirect): means that further operations are required to complete the request.
4xx (client error): indicates that there is a problem with the request and is caused by the client.
5xx (Server Error): indicates an error occurred inside the server.
Today we will focus on common 4xx and 5xx errors.
404 An error indicates that the requested resource does not exist on the server, usually because of a URL error or the page has been deleted. We can set the 404 status code using the following code:
<?php
// set up 404 Status code
header("HTTP/1.1 404 Not Found");
// You can add more error handling code here,For example, display a custom error page
echo "Page not found";
?>
This code sets the HTTP status code to 404 through the header() function and displays a simple error message through echo .
403 An error indicates that the server understands the request, but refuses to execute it. Usually this means permission issues, such as access to restricted pages. Here is the code for setting the 403 status code:
<?php
// set up 403 Status code
header("HTTP/1.1 403 Forbidden");
// You can add more error handling code here
echo "No access";
?>
This code will return a 403 status code, telling the browser user that there is no permission to access the current resource.
500 An error indicates an error occurred inside the server, usually because of an exception in the program or the server cannot process the request. Here is the code for setting the 500 status code:
<?php
// set up 500 Status code
header("HTTP/1.1 500 Internal Server Error");
// You can add more error handling code here
echo "Server Error,Please try again later";
?>
This code returns a 500 error code through the header() function and informs the user server that there is a problem.
By setting the appropriate HTTP status code, users can have a clearer understanding of what is going on. For example, if a page does not exist, returning a 404 will let the user know that their request cannot find the page, rather than displaying a default 500 error page. This transparency helps improve the user experience.
For search engines, it is also very important to return the correct HTTP status code. For example, returning a 404 status code can tell the search engine that the page no longer exists, avoiding the search engine from continuing to index the page. If 200 status codes are returned, the search engine may continue to crawl this page, causing unnecessary waste of resources.
When encountering problems, the appropriate HTTP status code can help developers quickly locate problems. For example, when a server has 500 errors, developers can check the server log to check the specific cause of the error and fix it in time.
In addition to setting the status code, the header() function can also be used to redirect or set cache control. Here are some common applications:
Use the header() function to redirect the user to another page. For example:
<?php
header("Location: https://m66.net/newpage.php");
exit();
?>
This will redirect the user to https://m66.net/newpage.php .
By setting the HTTP header, you can control how the browser caches the page. For example:
<?php
header("Cache-Control: no-cache, no-store, must-revalidate");
header("Pragma: no-cache");
header("Expires: 0");
?>
This will tell the browser not to cache the page and to request the latest content every time the page is loaded.
Mastering how to set status codes such as 404, 403, 500, etc. is an important skill in developing high-quality websites. Correct use of these status codes not only improves the user experience, but also helps search engines and developers better understand and deal with website problems. Hopefully today's article can help you better understand and use the header() function in PHP.