如何在不维护状态名称数组的情况下在 PHP 中发送状态代码?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4797274/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to send a status code in PHP, without maintaining an array of status names?
提问by NikiC
All I want to do, is send a 404
status code from PHP - but in a generic fashion. Both Router::statusCode(404)
and Router::statusCode(403)
should work, as well as any other valid HTTP status code.
我想要做的就是404
从 PHP发送一个状态代码 - 但以通用的方式。双方Router::statusCode(404)
并Router::statusCode(403)
应工作,以及任何其他有效的HTTP状态代码。
I do know, that you can specify a status code as third parameter to header
. Sadly this only works if you specify a string
. Thus calling header('', false, 404)
does notwork.
我知道,您可以将状态代码指定为header
. 遗憾的是,这仅在您指定string
. 因此,呼吁header('', false, 404)
并没有工作。
Furthermore I know, that one can send a status code via a header
call with a status line: header('HTTP/1.1 404 Not Found')
此外,我知道,可以通过header
带有状态行的呼叫发送状态代码:header('HTTP/1.1 404 Not Found')
But to do this I have to maintain an array of reason phrases (Not Found
) for all status codes (404
). I don't like the idea of this, as it somehow is a duplication of what PHP already does itself (for the third header
parameter).
但要做到这一点,我必须Not Found
为所有状态代码 ( 404
)维护一组原因短语( )。我不喜欢这个想法,因为它在某种程度上是 PHP 已经做的事情的重复(对于第三个header
参数)。
So, my question is: Is there any simple and clean way to send a status code in PHP?
所以,我的问题是:有什么简单而干净的方法可以在 PHP 中发送状态代码?
回答by hectorct
There is a new function for this in PHP >= 5.4.0 http_response_code
在 PHP >= 5.4.0 中有一个新函数 http_response_code
Simply do http_response_code(404)
.
简单地做http_response_code(404)
。
If you have a lower PHP version try header(' ', true, 404);
(note the whitespace in the string).
如果您的 PHP 版本较低,请尝试header(' ', true, 404);
(注意字符串中的空格)。
If you want to set the reason phrase as well try:
如果您还想设置原因短语,请尝试:
header('HTTP/ 433 Reason Phrase As You Wish');
回答by Marc B
The actual text of the code is irrelevant. You could do
代码的实际文本是无关紧要的。你可以做
header('The goggles, they do nawtink!', true, 404);
and it'd still be seen as a 404 by the browser - it's the code that matters.
它仍然会被浏览器视为 404 - 重要的是代码。
回答by Mike B
Zend Framework has a packaged solution in Zend_Http_Response
Zend Framework 有一个打包的解决方案 Zend_Http_Response
Zend_Http_Response::$messages
contains:
Zend_Http_Response::$messages
包含:
/**
* List of all known HTTP response codes - used by responseCodeAsText() to
* translate numeric codes to messages.
*
* @var array
*/
protected static $messages = array(
// Informational 1xx
100 => 'Continue',
101 => 'Switching Protocols',
// Success 2xx
200 => 'OK',
201 => 'Created',
202 => 'Accepted',
203 => 'Non-Authoritative Information',
204 => 'No Content',
205 => 'Reset Content',
206 => 'Partial Content',
// Redirection 3xx
300 => 'Multiple Choices',
301 => 'Moved Permanently',
302 => 'Found', // 1.1
303 => 'See Other',
304 => 'Not Modified',
305 => 'Use Proxy',
// 306 is deprecated but reserved
307 => 'Temporary Redirect',
// Client Error 4xx
400 => 'Bad Request',
401 => 'Unauthorized',
402 => 'Payment Required',
403 => 'Forbidden',
404 => 'Not Found',
405 => 'Method Not Allowed',
406 => 'Not Acceptable',
407 => 'Proxy Authentication Required',
408 => 'Request Timeout',
409 => 'Conflict',
410 => 'Gone',
411 => 'Length Required',
412 => 'Precondition Failed',
413 => 'Request Entity Too Large',
414 => 'Request-URI Too Long',
415 => 'Unsupported Media Type',
416 => 'Requested Range Not Satisfiable',
417 => 'Expectation Failed',
// Server Error 5xx
500 => 'Internal Server Error',
501 => 'Not Implemented',
502 => 'Bad Gateway',
503 => 'Service Unavailable',
504 => 'Gateway Timeout',
505 => 'HTTP Version Not Supported',
509 => 'Bandwidth Limit Exceeded'
);
Even if you're not using zend-framework you might be able to break this out for personal use.
即使您不使用 zend-framework,您也可以将其分解以供个人使用。
回答by Brad
Yeah, just do this...
是的,就这样做...
header('x', true, 404);
The first string parameter can be anything that doesn't contain a :
. PHP will then replace and go with the standard phrase. The second parameter specifies "always replace", and the 3rd is the status code you want.
第一个字符串参数可以是任何不包含:
. PHP 将替换并使用标准短语。第二个参数指定“总是替换”,第三个是你想要的状态码。
References:
参考: