HTTP强制设置状态码
方法
在最后的程序執行完畢之前,加入下列語句,即可實現所有的返回碼都為200
即使在服務器內部發生錯誤,會報500情況下
只要加上register_shutdown_function函數的處理
同樣可以實現返回200的效果:header("HTTP/1.0 200 OK");
或者返回999等待,都可以的:header("HTTP/1.1 999 666666");
目的
(對用戶更加友好的處理方式)? 主要是為了屏蔽錯誤,
把錯誤日志輸出到錯誤日志中,同時保證返回正常信息或者跳轉到其他頁面鏈接
register_shutdown_function
define
—Register a function for execution on shutdown? 注明一個函數在關閉的時候進行執行 在腳本結束或者exit()的時候執行
void register_shutdown_function ( callable $callback [, mixed $parameter [, mixed $... ]] )
Registers a callback to be executed after script execution finishes or exit() is called.
Multiple calls to register_shutdown_function() can be made, and each will be called in the same order as they were registered. If you call exit() within one registered shutdown function, processing will stop completely and no other registered shutdown functions will be called.
可以多次調用此關閉函數,每一個關閉函數都可以被調用? 按照他們注明時候的順序 , 如果你在關閉函數之內進行執行exit()? 進程將會完全停止? 不會調用其他的關閉函數。
parameter
callback回調函數
The shutdown callback to register.聲明關閉時候的回調函數
The shutdown callbacks are executed as the part of the request, so it's possible to send output from them and access output buffers.? shutdown函數回調作為請求的一部分執行,因此他可以從回調函數中進行輸出? 同時也可以進行訪問輸出緩沖區。
parameter附加參數
It is possible to pass parameters to the shutdown function by passing additional parameters.
...可以通過傳遞附加參數的方法進行傳遞參數給shutdown函數
<?php //關閉函數 function shutdown() {// This is our shutdown function, in // here we can do any last operations// before the script is complete.// 類似于golang的恐慌函數 panic()echo 'Script executed with success', PHP_EOL; }//注冊關閉函數 register_shutdown_function('shutdown'); ?>狀態碼
//設置HTTP狀態嗎 public static function HTTP($num) {$http = array(100 => "HTTP/1.1 100 Continue",101 => "HTTP/1.1 101 Switching Protocols",200 => "HTTP/1.1 200 OK",201 => "HTTP/1.1 201 Created",202 => "HTTP/1.1 202 Accepted",203 => "HTTP/1.1 203 Non-Authoritative Information",204 => "HTTP/1.1 204 No Content",205 => "HTTP/1.1 205 Reset Content",206 => "HTTP/1.1 206 Partial Content",300 => "HTTP/1.1 300 Multiple Choices",301 => "HTTP/1.1 301 Moved Permanently",302 => "HTTP/1.1 302 Found",303 => "HTTP/1.1 303 See Other",304 => "HTTP/1.1 304 Not Modified",305 => "HTTP/1.1 305 Use Proxy",307 => "HTTP/1.1 307 Temporary Redirect",400 => "HTTP/1.1 400 Bad Request",401 => "HTTP/1.1 401 Unauthorized",402 => "HTTP/1.1 402 Payment Required",403 => "HTTP/1.1 403 Forbidden",404 => "HTTP/1.1 404 Not Found",405 => "HTTP/1.1 405 Method Not Allowed",406 => "HTTP/1.1 406 Not Acceptable",407 => "HTTP/1.1 407 Proxy Authentication Required",408 => "HTTP/1.1 408 Request Time-out",409 => "HTTP/1.1 409 Conflict",410 => "HTTP/1.1 410 Gone",411 => "HTTP/1.1 411 Length Required",412 => "HTTP/1.1 412 Precondition Failed",413 => "HTTP/1.1 413 Request Entity Too Large",414 => "HTTP/1.1 414 Request-URI Too Large",415 => "HTTP/1.1 415 Unsupported Media Type",416 => "HTTP/1.1 416 Requested range not satisfiable",417 => "HTTP/1.1 417 Expectation Failed",500 => "HTTP/1.1 500 Internal Server Error",501 => "HTTP/1.1 501 Not Implemented",502 => "HTTP/1.1 502 Bad Gateway",503 => "HTTP/1.1 503 Service Unavailable",504 => "HTTP/1.1 504 Gateway Time-out");header($http[$num]); }實戰
<?phpfunction setHeader(){echo "111";header("HTTP/1.1 401 Unauthorized");} register_shutdown_function('setHeader');總結
以上是生活随笔為你收集整理的HTTP强制设置状态码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 女孩骨龄偏大怎么办?有哪些可以快速长高1
- 下一篇: PHP的静态方法与普通方法