设置PHP中的HTTP响应code(Apache下) [英] Setting a HTTP response code in PHP (under Apache)

查看:123
本文介绍了设置PHP中的HTTP响应code(Apache下)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

鉴于设置在PHP HTTP响应code(具体地,Apache下)以下两种方法:

方法1:

  http_response_ code(404);

方法2:

 标题(HTTP / 1.0 404未找​​到);

我的问题是:


  1. 除了一个事实,即 http_response_ code 仅在PHP 5.4或更高版本可用,什么是这两种方法之间的差异为什么/时,使用一个比其他?

  2. 在什么地方原因短语来自当使用第一实施例? (我已经检查和原因短语的的从什么地方产生)


解决方案

由于我正在downvoted湮没无明显原因,我已经成功通过了PHP源代码code淘到这一回答自己。希望这可以作为其他任何人想工作了这一点参考。

这两种方法基本上是功能上等同的。 http_response_ code基本上是写一个HTTP状态的头,用增加的奖金的简写方式,PHP将制定出一个合适的理由短语在它维护一个枚举您回应code匹配的值之一,以提供在<一个href=\"https://github.com/php/php-src/blob/fc33f52d8c25997dd0711de3e07d0dc260a18c11/main/http_status_$c$cs.h\"相对=nofollow> PHP-的src / main / http_status_ codes.h 。请注意,这意味着你的回应code必须响应code的PHP知道匹配。使用这种方法,你不能创建自己的响应codeS,但是你可以使用头方法。

总结 - 设置响应codeShttp_response_ code和头之间的不同:

1。使用http_response_ code会导致PHP搭配和词组的原因是硬coded到PHP源$ C ​​$ C列表应用简短原因。

2。由于上述第1点中,如果使用http_response_ code,你必须设置一个code PHP知道的。你不能设置自定义的code,但您可以设置自定义code(和原因短语)如果你使用的标题方法。

Given the following two methods for setting a HTTP response code in PHP (specifically, under Apache):

Method 1:

http_response_code(404);

Method 2:

header("HTTP/1.0 404 Not Found");

My questions are:

  1. Aside from the fact that http_response_code is only available in PHP 5.4 or greater, what are the differences between the two approaches and why/when to use one over the other?
  2. Where does the Reason Phrase come from when using the first example? (I've checked and a Reason Phrase is generated from somewhere)

解决方案

Since I'm being downvoted into oblivion for no apparent reason, I've managed to answer this myself by scouring through the PHP source code. Hopefully this serves as a reference for anyone else trying to work this out.

The two methods are essentially functionally equivalent. http_response_code is basically a shorthand way of writing a http status header, with the added bonus that PHP will work out a suitable Reason Phrase to provide by matching your response code to one of the values in an enumeration it maintains within php-src/main/http_status_codes.h. Note that this means your response code must match a response code that PHP knows about. You can't create your own response codes using this method, however you can using the header method.

In summary - The differences between "http_response_code" and "header" for setting response codes:

1. Using http_response_code will cause PHP to match and apply a Reason Phrase from a list of Reason Phrases that are hard-coded into the PHP source code.

2. Because of point 1 above, if you use http_response_code you must set a code that PHP knows about. You can't set your own custom code, however you can set a custom code (and Reason Phrase) if you use the header method.

这篇关于设置PHP中的HTTP响应code(Apache下)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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