int header
(string string);Header 関数は、HTML ファイル の送信に先立って、生の HTTP ヘッダ文字列を送信 するために使用します。生の HTTP ヘッダの詳細は、 HTTP 1.1 Specificationを参照して 下さい。 注意: 覚えておいて頂きたいのは、Header 関数は、 通常の HTML タグまたは PHP からの出力にかかわらず、すべての実際の出力 の前にコールされる必要があることです。 頻出するエラーとして、include または auto_prepend を有するコードで空白または空行があると header がコールされる前に強制的に出力が 行われてしまうというものがあります。
header をコールする場合に特別な場合が2つあります。 There are two special-case header calls. The first is the "Location" header. Not only does it send this header back to the browser, it also returns a REDIRECT status code to Apache. From a script writer's point of view this should not be important, but for people who understand Apache internals it is important to understand.
header("Location: http://www.php.net"); /* ブラウザをPHP Web サイトにリダイレクトする */ exit; /* リダイレクトを行う際に以下のコードが実行されないようにする */
The second special-case is any header that starts with the string, "HTTP/" (case is not significant). For example, if you have your ErrorDocument 404 Apache directive pointed to a PHP script, it would be a good idea to make sure that your PHP script is actually generating a 404. The first thing you do in your script should then be:
header("http/1.0 404 Not Found");
PHP スクリプトはしばしば動的に HTML を生成するため、 クライアントブラウザや サーバーおよびクライアントブラウザの間でプロキシがキャッシュを行ったり するべきではありません。 多くのプロキシとクライアントは、以下のコードにより 強制的にキャッシュを無効にできます。
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // always modified header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1 header("Pragma: no-cache"); // HTTP/1.0