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

查看:39
本文介绍了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");

然而,这需要对 (Fast)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 函数具有第三个参数,可让您轻松设置响应代码,但使用它需要第一个参数为非空字符串.这里有两个选项:

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);

我推荐第二个.第一个确实适用于我测试过的所有浏览器,但一些较小的浏览器或网络爬虫可能会遇到标题行仅包含冒号的问题.标题字段名称在 2 中.变体当然没有以任何方式标准化,可以修改,我只是选择了一个有希望的描述性名称.

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.

http_response_code() 函数是在 PHP 5.4 中引入的,它让事情很多变得更容易.

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天全站免登陆