HTTP函式庫

header

(PHP3 , PHP4)

header ---  送出HTTP標頭

語法 : int header (string string)

說明 : 

header( )函式是用在HTML檔案的上方,來送出HTTP標頭字串。注意 : header( )必須在任何標準的HTML標籤或是從PHP輸出之前來呼叫此函式。

這裡有二個標頭呼叫的特殊例子,第一個是"header"標頭,它可以引導瀏灠器到新的地方

Example :

<?php

    header ("Location: http://www.php.net"); /* Redirect browser 
                                                         to PHP web site */
    exit;                                         /* Make sure that code below does 
                                                        not get executed when we redirect. */

?>      

第二個特殊的例子是任何的標頭都以字串"HTTP/"開始。例如 : 如果你有ErrorDocument 404 Apache指令指到一個PHP程式,這將是個好主意,來確定你的PHP程式真的可以產生404。你的程式碼將會是 :

Example :

<?php

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

?>

PHP程式通常是用來產生動態的HTML,有時候必須使它不被用戶端(client)的瀏灠器或是任何proxy(介於伺服器與用戶端)儲存起來,許多的proxies和client可以使用下列方式來迫使無法儲存(cache)。

<?php

    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

?>      

參考 : header_sent( )


上一頁 首頁 下一頁