PHPヘッダー()関数
例
ページのキャッシュを防ぐために、3つのHTTPヘッダーを送信します。
<?php
// Date in the past
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Cache-Control: no-cache");
header("Pragma: no-cache");
?>
<html>
<body>
...
...
注:ユーザーがブラウザーのデフォルトのキャッシュ設定を変更するために設定できるオプションがあります。上記のヘッダーを送信することで、これらの設定のいずれかを上書きし、ブラウザにキャッシュしないように強制します。
定義と使用法
header()関数は、生のHTTPヘッダーをクライアントに送信します。
実際の出力を送信する前に、header()関数を呼び出す必要があることに注意してください。
構文
header(header, replace, http_response_code)
パラメータ値
Parameter | Description |
---|---|
header | Required. Specifies the header string to send |
replace | Optional. Indicates whether the header should replace a previous similar header or add a new header of the same type. Default is TRUE (will replace). FALSE allows multiple headers of the same type |
http_response_code | Optional. Forces the HTTP response code to the specified value |
技術的な詳細
戻り値: | 何もない |
---|---|
PHPバージョン: | 4.0以降 |
PHP変更ログ: | PHP 5.1.2:複数のヘッダーが同時に送信されないようになりました。これはヘッダーインジェクション攻撃に対する保護です |
その他の例
例
生成されたPDFファイルを保存するようにユーザーに促します(Content-Dispositionヘッダーを使用して、推奨ファイル名を指定し、ブラウザーに保存ダイアログボックスを表示させます)。
<?php
header("Content-type:application/pdf");
// It will be called downloaded.pdf
header("Content-Disposition:attachment;filename='downloaded.pdf'");
// The PDF source is in original.pdf
readfile("original.pdf");
?>
<html>
<body>
...
...
❮PHPネットワークリファレンス