PHP:如何发送HTTP响应代码? [英] PHP: How to send HTTP response code?

查看:213
本文介绍了PHP:如何发送HTTP响应代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个PHP脚本,需要使用HTTP响应代码(状态代码)进行响应,例如HTTP 200 OK,或一些4XX或5XX代码.

I have a PHP script that needs to make responses with HTTP response codes (status-codes), like HTTP 200 OK, or some 4XX or 5XX code.

如何在PHP中做到这一点?

How can I do this in PHP?

推荐答案

我刚刚发现了这个问题,并认为它需要更全面的答案:

I just found this question and thought it needs a more comprehensive answer:

PHP 5.4 开始,有三种方法可以实现此目的:

As of PHP 5.4 there are three methods to accomplish this:

header() 函数有一个特殊的用例,可以检测HTTP响应行并让您替换它自定义一个

The header() function has a special use-case that detects a HTTP response line and lets you replace that with a custom one

header("HTTP/1.1 200 OK");

但是,这需要对(快速)CGI PHP进行特殊处理:

However, this requires special treatment for (Fast)CGI PHP:

$sapi_type = php_sapi_name();
if (substr($sapi_type, 0, 3) == 'cgi')
    header("Status: 404 Not Found");
else
    header("HTTP/1.1 404 Not Found");

注意:根据 HTTP RFC 原因短语可以是任何自定义字符串(符合标准),但是出于客户端兼容性的考虑,我建议放置随机字符串在那里.

Note: According to the HTTP RFC, the reason phrase can be any custom string (that conforms to the standard), but for the sake of client compatibility I do not recommend putting a random string there.

注意: php_sapi_name() 需要 PHP 4.0 .1

使用第一个变体时显然存在一些问题.我认为其中最大的一点是它是由PHP或Web服务器部分解析的,且文档记录不充分.

There are obviously a few problems when using that first variant. The biggest of which I think is that it is partly parsed by PHP or the web server and poorly documented.

从4.3开始,header函数具有第3个参数,可让您稍微舒适地设置响应代码,但使用它需要第一个参数为非空字符串.这是两个选项:

Since 4.3, the header function has a 3rd argument that lets you set the response code somewhat comfortably, but using it requires the first argument to be a non-empty string. Here are two options:

header(':', true, 404);
header('X-PHP-Response-Code: 404', true, 404);

我推荐第二个..第一个可以在我测试过的所有浏览器上都可以运行,但是某些次要浏览器或Web爬网程序可能会出现标题行仅包含冒号的问题.标头字段名称在第二位.当然,变体没有任何标准化形式,可以修改,我只是选择了一个描述性的名称.

I recommend the 2nd one. The first does work on all browsers I have tested, but some minor browsers or web crawlers may have a problem with a header line that only contains a colon. The header field name in the 2nd. variant is of course not standardized in any way and could be modified, I just chose a hopefully descriptive name.

PHP 5.4中引入了 http_response_code() 函数,它使事情变得 更容易.

The http_response_code() function was introduced in PHP 5.4, and it made things a lot easier.

http_response_code(404);

仅此而已.

当我需要低于5.4的兼容性但想要新" http_response_code函数的功能时,这是我已经准备的函数.我相信PHP 4.3足够向后兼容,但您永远不知道...

Here is a function that I have cooked up when I needed compatibility below 5.4 but wanted the functionality of the "new" http_response_code function. I believe PHP 4.3 is more than enough backwards compatibility, but you never know...

// For 4.3.0 <= PHP <= 5.4.0
if (!function_exists('http_response_code'))
{
    function http_response_code($newcode = NULL)
    {
        static $code = 200;
        if($newcode !== NULL)
        {
            header('X-PHP-Response-Code: '.$newcode, true, $newcode);
            if(!headers_sent())
                $code = $newcode;
        }       
        return $code;
    }
}

这篇关于PHP:如何发送HTTP响应代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆