Home > Backend Development > PHP Problem > How to return error code in php

How to return error code in php

藏色散人
Release: 2023-03-12 12:44:02
Original
2530 people have browsed it

In PHP, you can use the header() function to return an error code, which means adding 404 header information to the page. The syntax is such as "header("HTTP/1.0 404 Not Found");".

How to return error code in php

The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer

How does php return an error code?

Using the header() function in PHP, you can add 404 header information to the returned page, thus prompting the browser that the web page cannot be found.

So you can use:

header("HTTP/1.0 404 Not Found");
Copy after login

or:

header("Status: 404 Not Found");
Copy after login

The latter is used in FastCGI mode. You can write the two sentences directly at the same time in the php code.

Extract the header usage instructions from the official website of the PHP manual as follows:

The header string.There are two special-case header calls. The first is a header that starts with the string "HTTP/" (case is not significant), which will be used to figure out the HTTP status code to send. For example, if you have configured Apache to use a PHP script to handle requests for missing files (using the ErrorDocument directive), you may want to make sure that your script generates the proper status code.For FastCGI you must use the following for a 404 response:
Copy after login

The header("Location: xxx.com"); defaults to a 302 status jump, so it cannot be browsed The controller outputs a 404 error status.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to return error code in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template