SOFTELメモ Developer's blog

会社概要 ブログ 調査依頼 採用情報 ...
技術者募集中

【php】HTTPヘッダ送出の書き方例一覧

// あえて200で上書きしたいときなどに
header('HTTP/1.1 200 OK');

// ページが見つかりませんでした
header('HTTP/1.1 404 Not Found');

// アクセスできません
header('HTTP/1.1 403 Forbidden');

// 恒久的移動 - Webサイトのお引越しなどの場合はこれ
header('HTTP/1.1 301 Moved Permanently');

// サーバーエラー
header('HTTP/1.1 500 Internal Server Error');

// HTTPリダイレクト
header('Location: http://www.example.org/');

// METAタグでよくやるRefreshをHTTPヘッダでおこなう
// <meta http-equiv="refresh" content="10;http://www.example.org/ />
// なおRefreshヘッダはRFCにないらしい
header('Refresh: 10; url=http://www.example.org/');
print '10秒後に http://www.example.org/ に移動します';

// override X-Powered-By value
header('X-Powered-By: PHP/9.8.7');
header('X-Powered-By: Softel');

// 言語 (en = English、ja = Japanese)
header('Content-language: en');

// 最終更新日 (キャッシュさせたいときなどに)
$time = time() - 60; // 他 filemtime($fn) など
header('Last-Modified: '.gmdate('D, d M Y H:i:s', $time).' GMT');

// 変更なし → キャッシュを使ってください
header('HTTP/1.1 304 Not Modified');

// コンテンツの長さ(キャッシュさせたいときなどに)
header('Content-Length: 1234');

// ダウンロード用
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="example.zip"'); 
header('Content-Transfer-Encoding: binary');
// ヘッダの後にファイルの内容を送出
readfile('example.zip');

// キャッシュをさせないようにする
header('Cache-Control: no-cache, no-store, max-age=0, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // 過去の日付
header('Pragma: no-cache');

// 各種Content-Type
header('Content-Type: text/html; charset=iso-8859-1');
header('Content-Type: text/html; charset=utf-8');
header('Content-Type: text/plain'); // テキスト
header('Content-Type: image/jpeg'); // JPG画像
header('Content-Type: application/zip'); // ZIPファイル
header('Content-Type: application/pdf'); // PDFファイル
header('Content-Type: audio/mpeg'); // Audio MPEG (MP3,...) ファイル
header('Content-Type: application/x-shockwave-flash'); // Flash

// ベーシック認証の小窓を表示する
header('HTTP/1.1 401 Unauthorized');
header('WWW-Authenticate: Basic realm="Top Secret"');
print '認証をキャンセルもしくは間違ったログイン情報を入力した場合、';
print 'この内容が表示されます。';

参考

PHPマニュアル – header() 関数

関連するメモ

コメント