HTTP强制设置状态码

PHP返回码定制与优雅错误处理

方法

在最后的程序执行完毕之前,加入下列语句,即可实现所有的返回码都为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]);
}

实战

<?php

function setHeader(){
	echo "111";
	header("HTTP/1.1 401 Unauthorized");

}
register_shutdown_function('setHeader');

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值