Source for file http_response_header.php
Documentation is available at http_response_header.php
* HTTP Response Header Class
* HTTP Response Header ClassはHTTP Responseをコントロールするクラスです。
* @package http_response_header
* @author http://purl.org/meta/me/
* @version http_response_header.php 1.0 2011-08-20
* @license Creative Commons License CC BY (http://creativecommons.org/licenses/by/3.0/)
* HTTP Status Codeの一覧。(ja)
* List of HTTP Status Code.(en)
static $httpStatusCodes =
array(
101 =>
'Switching Protocols',
203 =>
'Non-Authoritative Information',
206 =>
'Partial Content',
300 =>
'Multiple Choices',
301 =>
'Moved Permanently',
307 =>
'Temporary Redirect',
402 =>
'Payment Required',
405 =>
'Method Not Allowed',
407 =>
'Proxy Authentication Required',
408 =>
'Request Time-out',
411 =>
'Length Required',
412 =>
'Precondition Failed',
413 =>
'Request Entity Too Large',
414 =>
'Request-URI Too Large',
415 =>
'Unsupported Media Type',
416 =>
'Requested range not satisfiable',
417 =>
'Expectation Failed',
500 =>
'Internal Server Error',
501 =>
'Not Implemented',
503 =>
'Service Unavailable',
504 =>
'Gateway Time-out',
505 =>
'HTTP Version Not Supported',
509 =>
'Bandwidth Limit Exceeded'
static $httpVersion =
'1.0';
* Available HTTP Versions.(en)
static $httpVersions =
array('1.0', '1.1');
* Set the expiration date.(en)
* HttpResponseHeader::expires(60 * 60);
* # 以下のようにレスポンスヘッダーを出力します。
* # Cache-Control: max-age=3600
* # Expires: Mon, 29 Aug 2011 10:31:06 GMT
* @param integer $time 有効期限までの秒数(ja) Expiration Seconds(en)
self::header('Cache-Control', 'max-age=' .
$time);
self::header('Expires', $time +
time());
* タイムスタンプをRFC 1123の日付形式に変換します。(ja)
* HttpResponseHeader::date(1314600068);
* # Mon, 29 Aug 2011 06:41:08 GMT
* @param integer $timestamp タイムスタンプ(ja) Timestamp(en)
* @return string RFC 1123の日付形式(ja) RFC 1123 date format(en)
function date($timestamp =
null) {
return gmdate('D, d M Y H:i:s', $timestamp) .
' GMT';
* 既に出力したHTTP Response Headerを取得します。(ja)
* Get the HTTP Response Header output now.(en)
* @param mixed $name 取得したいフィールドを指定します。全てのフィールドを取得したい場合値は空でよいです。(ja) Specify the field to get.(en)
* @return mixed 文字列または配列(ja) string or array(en)
foreach($headersList as $key =>
$value) {
$array[][$matches[1]] =
$matches[2];
if(isset
($headers[$name])) {
* リクエストされたHTTPのバージョン。(ja)
* Version of the HTTP request.(en)
* @return string HTTPのバージョン(ja) HTTP Version(en)
if (isset
($_SERVER['SERVER_PROTOCOL'])) {
return substr($_SERVER['SERVER_PROTOCOL'], -
3);
return self::$httpVersion;
* Start the encoding of gzip.(en)
* HttpResponseHeader::gzip();
* # 以下のようにレスポンスヘッダーを出力します。
* # Vary: Accept-Encoding
* # Content-Encoding: gzip
* @return mixed 成功した場合はContent-Encoding Headerの内容を、失敗した場合はfalseを返します。
ob_start('ob_gzhandler');
self::header('Vary', 'Accept-Encoding', false);
if(isset
($_SERVER['HTTP_ACCEPT_ENCODING'])) {
$encodings =
explode(',', $_SERVER['HTTP_ACCEPT_ENCODING']);
foreach($encodings as $value) {
self::header('Content-Encoding', $value);
* HTTP Response Headerを出力します。(ja)
* Print the HTTP Response Header(en)
* HttpResponseHeader::header(array(
* 'ETag' => 'Etag Field value',
* 'Content-MD5' => 'Content-MD5 Field value',
* 'Last-Modified' => 1314600068
* # 以下のようにレスポンスヘッダーを出力します。
* # Etag: "d4289a42770d87be5a78cbcefcc1e3b76a07f94dd1005020736b6943e0e65bf07d97472739fb775ea7a8107ce024864def444d935f494e096f2bfd0382bb2e84"
* # Content-MD5: RhQI+teVkAukVCltE3K+yg==
* # Last-Modified: Mon, 29 Aug 2011 06:41:08 GMT
* @param mixed $headers Headerの配列またはフィールド名(ja) Headers array or field name(en)
* @param mixed $replace 前に送信された類似のヘッダを置換するか。$headersがフィールド名の場合フィールドの内容
* @param boolean $_replace
function header($headers, $replace =
true, $_replace =
true) {
$headers =
array($headers =>
$replace);
foreach($headers as $key =>
$value) {
foreach($value as $_key =>
$_value) {
if($firstKey ===
$_key) {
self::header($key, $_value, $replace);
self::header($key, $_value, false);
if(($key ===
'Date' ||
$key ===
'Expires' ||
$key ===
'Last-Modified') &&
(is_numeric($value) ||
is_null($value))) {
$value =
self::date($value);
} elseif($key ===
'ETag' &&
!preg_match('/^(W\/)?"[a-zA-Z0-9\-]+"$/', $value)) {
$value =
'"' .
hash('sha512', $value) .
'"';
} elseif($key ===
'Content-MD5' &&
strlen($value) !==
24) {
header($key .
': ' .
$value, $replace);
* HTML用のHeaderを出力します。(ja)
* Print the Header for HTML.(en)
* HttpResponseHeader::htmlHeader();
* # 以下のようにレスポンスヘッダーを出力します。
* # Content-Style-Type: text/css
* # Content-Script-Type: text/javascript
'Content-Style-Type' =>
'text/css',
'Content-Script-Type' =>
'text/javascript',
* Webブラウザーがキャッシュしない様に指示します。(ja)
* Instructs the Web browser not to cache.(en)
* HttpResponseHeader::noCache();
* # 以下のようにレスポンスヘッダーを出力します。
* # Expires: Mon, 26 Jul 1997 05:00:00 GMT
* # Last-Modified: Mon, 29 Aug 2011 07:15:22 GMT
* # Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
'Expires' =>
'Mon, 26 Jul 1997 05:00:00 GMT',
'Cache-Control' =>
array(
'no-store, no-cache, must-revalidate',
'post-check=0, pre-check=0'
* 条件が一致すれば"304 Not Modified"または"412 Precondition Failed"を出力します。(ja)
* Print the "304 Not Modified" or "412 Precondition Failed" if the conditions are matched.(en)
* @param boolean $exit PHPを終了するならばtrue(ja) If True, the closing PHP(en)
$headers =
self::getHeader();
if(isset
($headers['ETag']) && isset
($_SERVER['HTTP_IF_MATCH']) &&
$headers['ETag'] !==
$_SERVER['HTTP_IF_MATCH']) {
} elseif(isset
($headers['Last-Modified']) && isset
($_SERVER['HTTP_IF_UNMODIFIED_SINCE']) &&
strtotime($_SERVER['HTTP_IF_UNMODIFIED_SINCE']) !==
strtotime($headers['Last-Modified'])) {
if(isset
($headers['ETag']) && isset
($_SERVER['HTTP_IF_NONE_MATCH']) &&
$headers['ETag'] ===
$_SERVER['HTTP_IF_NONE_MATCH']) {
if(isset
($_SERVER['REQUEST_METHOD']) &&
$_SERVER['REQUEST_METHOD'] ===
'GET' ||
$_SERVER['REQUEST_METHOD'] ===
'HEAD') {
} elseif(isset
($headers['Last-Modified']) && isset
($_SERVER['HTTP_IF_MODIFIED_SINCE']) &&
strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) ===
strtotime($headers['Last-Modified'])) {
* パブリックなキャッシュをするように指示します。(ja)
* HttpResponseHeader::publicCache();
* # 以下のようにレスポンスヘッダーを出力します。
* # Cache-Control: public
'Cache-Control' =>
'public',
* Corresponds to a Range Request.(en)
function range($content =
null) {
if(isset
($_SERVER['HTTP_RANGE']) &&
preg_match('/^bytes=([0-9]+)?-([0-9]+)?$/', $_SERVER['HTTP_RANGE'], $matches)) {
$headers =
self::getHeader();
if(isset
($_SERVER['HTTP_IF_RANGE'])) {
if(preg_match('/^(W\/)?"[a-zA-Z0-9\-]+"$/', $_SERVER['HTTP_IF_RANGE']) && isset
($headers['ETag']) &&
$headers['ETag'] !==
$_SERVER['HTTP_IF_RANGE']) {
} elseif(isset
($headers['Last-Modified']) &&
strtotime($_SERVER['HTTP_IF_RANGE']) !==
strtotime($headers['Last-Modified'])) {
} elseif(!isset
($headers['ETag']) &&
!isset
($headers['Last-Modified'])) {
if(isset
($matches[1]) &&
$matches[1] ===
'') {
if(isset
($matches[2]) &&
$matches[2] ===
'') {
if(isset
($matches[1]) && isset
($matches[2])) {
$length =
$matches[2] -
$offset +
1;
if(isset
($matches[1]) &&
!isset
($matches[2])) {
} elseif(!isset
($matches[1]) && isset
($matches[2])) {
$offset =
$size -
$matches[2];
if($offset >=
$size ||
$offset >
$total) {
self::header('Content-Range', 'bytes ' .
$offset .
'-' .
$total .
'/' .
$size);
$content =
substr($content, $offset, $length);
* Print the Server information.(en)
* HttpResponseHeader::server('Apache/2.2');
* # 以下のようにレスポンスヘッダーを出力します。
* @return boolean 成功ならばtrueを、失敗ならばfalseを返す。
function server($content =
null) {
self::header('Server', $content);
} elseif(isset
($_SERVER['SERVER_SOFTWARE'])) {
self::header('Server', $_SERVER['SERVER_SOFTWARE']);
* HTTP Status Codeを出力します。(ja)
* Print the HTTP Status Code.(en)
* HttpResponseHeader::statusCode(404);
* # 以下のようにレスポンスヘッダーを出力します。
* # HTTP/1.1 404 Not Found
* @param integer $status HTTP Status Codeの番号(ja) The number of HTTP Status Code(en)
* @param string $httpVersion HTTPのバージョン(ja) HTTP Version(en)
* @return boolean 成功ならばtrueを、失敗ならばfalseを返します。
function statusCode($status =
200, $httpVersion =
null) {
$httpVersion =
self::getHttpVersion();
if(isset
(self::$httpStatusCodes[$status])) {
header('HTTP/' .
$httpVersion .
' ' .
$status .
' ' .
self::$httpStatusCodes[$status]);
Documentation generated on Mon, 29 Aug 2011 11:22:14 +0000 by phpDocumentor 1.4.3