Home  >  Article  >  Backend Development  >  PHP regular expression in action: matching HTTP status codes

PHP regular expression in action: matching HTTP status codes

WBOY
WBOYOriginal
2023-06-23 09:33:461342browse

PHP regular expression practice: matching HTTP status code

In website development, HTTP status code is a very important concept. According to the HTTP protocol, each HTTP response should contain a 3-digit status code to tell the client the processing result of the request. If you are developing websites using PHP, mastering regular expressions to match HTTP status codes is a useful skill.

Classification of HTTP status codes

HTTP status codes are divided into the following five categories according to the first digit:

  • 1xx: Informational status code, indicating that the server has received The request was made, but further action is required to complete processing.
  • 2xx: Success status code, indicating that the server has successfully processed the request.
  • 3xx: Redirect status code, indicating that further operations from the client are required to complete the request.
  • 4xx: Client error status code, indicating that there is a problem with the request sent by the client, causing the server to be unable to process it.
  • 5xx: Server error status code, indicating that an error occurred when the server processed the request.

In this article, we will first list all HTTP status codes, and then match them through PHP regular expressions.

HTTP status code list

The following are all HTTP status codes and their meanings:

  • 100 Continue: The client should continue sending requests. This provisional response is used to inform the client that part of its request has been received by the server and has not yet been rejected. Continuing to send the request may complete the entire request or another part of the request.
  • 101 Switching Protocols: The server has received and understood the client's request and completed the protocol upgrade operation. The server will notify the client to communicate on another protocol via the Upgrade header.
  • 200 OK: The request has been successful, and the response header or data body expected by the request will be returned with this response.
  • 201 Created: The request was successful and the server created a new resource.
  • 202 Accepted: The server has accepted the request, but has not yet completed processing, and success cannot be guaranteed. Generally used for asynchronous processing.
  • 203 Non-Authoritative Information: The server has successfully processed the request, but the returned information may come from another source.
  • 204 No Content: The server successfully processed the request, but did not return any content.
  • 205 Reset Content: The server successfully processed the request, but does not need to return any content.
  • 206 Partial Content: The server successfully processed part of the request, and the returned content is the entity content of the range request.
  • 300 Multiple Choices: The resource requested by the client can be found in multiple locations, and the server returns a list of resources for the client to choose from.
  • 301 Moved Permanently: The resource requested by the client has been permanently moved to a new location, and the server returns a redirect URL to the client.
  • 302 Found: The resource requested by the client has been temporarily moved to a new location, and the server returns a redirected URL to the client.
  • 303 See Other: The resource requested by the client has another URI, and the GET method should be used to obtain the requested resource.
  • 304 Not Modified: The client sent a conditional request, and the server told the client that the resources in the browser cache can continue to be used.
  • 305 Use Proxy: The resources requested by the client must be accessed through the proxy.
  • 307 Temporary Redirect: The resource requested by the client has been temporarily moved to a new location, and the server returns a redirected URL to the client. This redirection method does not change the HTTP request method and message body.
  • 400 Bad Request: The request has a syntax error or the request cannot be understood by the server.
  • 401 Unauthorized: The request requires user verification.
  • 402 Payment Required: This status code is reserved for future use and indicates that electronic money may be used in the future, although it is not currently used. This status code is specifically designed for digital currencies to ensure adequate server support for new digital currencies in the future.
  • 403 Forbidden: The server refused the request.
  • 404 Not Found: The server cannot find the requested resource.
  • 405 Method Not Allowed: The HTTP method requested by the client is not allowed.
  • 406 Not Acceptable: The server cannot complete the request based on the content characteristics requested by the client.
  • 407 Proxy Authentication Required: The requester should use a proxy for authorization.
  • 408 Request Timeout: The request timed out.
  • 409 Conflict: The request cannot be completed due to a conflict with the current state of the requested resource.
  • 410 Gone: The requested resource is no longer available on the server.
  • 411 Length Required: The client did not set the Content-Length header in the request, and the server cannot process this request.
  • 412 Precondition Failed: The server failed to satisfy one or more of the preconditions given in the header fields of the request when validating them.
  • 413 Payload Too Large: The request entity is too large and the server cannot handle it.
  • 414 URI Too Long: The requested URI is too long and the server cannot handle it.
  • 415 Unsupported Media Type: The server cannot handle the media format attached to the request.
  • 416 Range Not Satisfiable: The range requested by the client is invalid and the server cannot process it.
  • 417 Expectation Failed: The server cannot satisfy the Expect request header information.
  • 421 Misdirected Request: This request is for a server that cannot generate a response. This can be because the resources these requests need to access are tainted or not fully published, or are only available on the local machine, factors that only apply to the specific client connected to their network.
  • 422 Unprocessable Entity: The request format is correct, but it cannot respond due to semantic errors.
  • 423 Locked: The current resource is locked.
  • 424 Failed Dependency: The current request failed due to a conflict with the previous request.
  • 425 Too Early: The request cannot be satisfied until the conditions required to load the resource are met.
  • 426 Upgrade Required: Client should switch to TLS/1.0.
  • 428 Precondition Required: This request requires a valid condition prerequisite.
  • 429 Too Many Requests: Too many requests.
  • 431 Request Header Fields Too Large: The request header exceeds the size limit.
  • 451 Unavailable For Legal Reasons: The request should not be processed. The request may contain prohibited information.
  • 500 Internal Server Error: The server encountered an unknown error.
  • 501 Not Implemented: The server does not support the feature requested by the client.
  • 502 Bad Gateway: The server, acting as a gateway or proxy, encountered an error while executing the request.
  • 503 Service Unavailable: The server is temporarily unable to process the request and can return a Retry-After header to the client.
  • 504 Gateway Timeout: The server, acting as a gateway or proxy, failed to receive a response from the upstream server in time when executing the request.
  • 505 HTTP Version Not Supported: The server does not support the HTTP protocol version used in the request.
  • 506 Variant Also Negotiates: The server has an internal configuration error that causes the request being processed to satisfy more than one negotiable response.
  • 507 Insufficient Storage: The server cannot store the content necessary to complete the request.
  • 508 Loop Detected: The server found itself stuck in an infinite loop while processing the request.
  • 510 Not Extended: The client needs to further extend the response, and the server has no corresponding solution.
  • 511 Network Authentication Required: The client requires authentication to use network resources.

PHP regular expression matches HTTP status code

Now, let’s try to use PHP regular expression to match HTTP status code. In PHP, we can use the preg_match() function to implement regular expression matching. The following is a sample code that matches the HTTP status code:

$status_code = '200'; // 要匹配的状态码
$pattern = '/^[1-5][0-9]{2}$/'; // HTTP状态码的正则表达式
if(preg_match($pattern, $status_code)) {
    echo '匹配成功!';
} else {
    echo '匹配失败!';
}

In the above code, we first define the status code to be matched as 200, and then define the regular expression of the HTTP status code as "/^ 1-5{2}$/”. This regular expression can match 3 digits, the first digit must be 1~5, and the remaining two digits can be 0~9. Finally, when using the preg_match() function for matching, if the return value is 1, it means the match is successful, and if the return value is 0, it means the match failed.

If you want to match multiple HTTP status codes, you can use the preg_match_all() function. The following is a sample code that matches all HTTP status codes:

$status_codes = array('200', '404', '500'); // 要匹配的状态码数组
$pattern = '/^[1-5][0-9]{2}$/'; // HTTP状态码的正则表达式
foreach($status_codes as $status_code) {
    if(preg_match($pattern, $status_code)) {
        echo $status_code.' 匹配成功!'."
";
    } else {
        echo $status_code.' 匹配失败!'."
";
    }
}

In the above code, we first define the array of status codes to match, and then use foreach() to loop through the array. In the loop, we use the preg_match() function to perform matching and output the matching results based on the return value.

Summary

HTTP status code is a very important concept in website development. Sometimes, we need to match HTTP status codes through regular expressions. In PHP, we can use the preg_match() function to implement regular expression matching. I hope that through this article, you can master the skills of matching HTTP status codes with PHP regular expressions.

The above is the detailed content of PHP regular expression in action: matching HTTP status codes. 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