-------------------------------------------------------------------------------- HTTP 相關(guān)函式庫 --------------------------------------------------------------------------------
本函式庫共有 2 個函式 header: 送出 HTTP 協(xié)定的標頭到瀏覽器 setcookie: 送出 Cookie 資訊到瀏覽器。
-------------------------------------------------------------------------------- 函式:header() --------------------------------------------------------------------------------
HTTP 相關(guān)函式庫
header 送出 HTTP 協(xié)定的標頭到瀏覽器
語法: int header(string string);
傳回值: 整數(shù)
函式種類: 網(wǎng)路系統(tǒng)
內(nèi)容說明
標頭 (header) 是伺服器以 HTTP 協(xié)定傳 HTML 資料到瀏覽器前所送出的字串,在標頭與 HTML 文件之間尚需空一行分隔。有關(guān) HTTP 的詳細說明,可以參考坊間的相關(guān)書籍或更詳細的 RFC 2068 官方文件(http://www.w3.org/Protocols/rfc2068/rfc2068)。在 PHP 中送回 HTML 資料前,需先傳完所有的標頭。
注意: 傳統(tǒng)的標頭一定包含下面三種標頭之一,并只能出現(xiàn)一次。
Content-Type: xxxx/yyyy Location: xxxx:yyyy/zzzz Status: nnn xxxxxx 在新的多型標頭規(guī)格 (Multipart MIME) 方可以出現(xiàn)二次以上。
使用范例
范例一: 本例用來重導(dǎo)使用者到 PHP 的官方網(wǎng)站。 <> Header("Location: http://www.php.net"); exit; ?>
范例二: 欲讓使用者每次都能得到最新的資料,而不是 Proxy 或 cache 中的資料,可以使用下列的標頭 header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); header("Last-Modified: " . gmdate("D, d M Y H:i:s") . "GMT"); header("Cache-Control: no-cache, must-revalidate"); header("Pragma: no-cache");
范例三: 讓使用者的瀏覽器出現(xiàn)找不到檔案的訊息。 <> header("Status: 404 Not Found"); ?>
范例四: bill@evil.inetarena.com (28-Apr-1999) 提供讓使用者下載檔案的范例。 header("Content-type: application/x-gzip"); header("Content-Disposition: attachment; filename=some-file.tar.gz"); header("Content-Description: PHP3 Generated Data");
-------------------------------------------------------------------------------- 函式:setcookie() --------------------------------------------------------------------------------
HTTP 相關(guān)函式庫
setcookie 送出 Cookie 資訊到瀏覽器。
語法: int setcookie(string name, string value, int expire, string path, string domain, int secure);
傳回值: 整數(shù)
函式種類: 網(wǎng)路系統(tǒng)
內(nèi)容說明
本函式會跟著標頭 Header 送出一段小資訊字串到瀏覽器。使用本函式要在送出 HTML 資料前,實際上 cookie 也算標頭的一部份。本函式的參數(shù)除了第一個 name 之外,都是可以省略的。參數(shù) name 表示 cookie 的名稱;value 表示這個 cookie 的值,這個參數(shù)為空字串則表示取消瀏覽器中該 cookie 的資料;expire 表示該 cookie 的有效時間;path 為該 cookie 的相關(guān)路徑;domain 表示 cookie 的網(wǎng)站;secure 則需在 https 的安全傳輸時才有效。想得到更多的 cookie 資訊可以到 http://www.netscape.com/newsref/std/cookie_spec.html,由 cookie 原創(chuàng)者 Netscape 所提供的完整資訊。
使用范例
dante@mpath.com (27-May-1999) 所提供的 setcookie() 及 header() 范例。
<> $status = 0; if (isset($myTstCky) && ($myTstCky == "ChocChip")) $status = 1; if (!isset($CCHK)) { setcookie("myTstCky", "ChocChip"); header("Location: $PHP_SELF?CCHK=1"); exit; } ?>
Cookie Check
Cookie Check Status: <> printf ('%s ;', $status ? "00FF00" : "FF0000", $status ? "PASSED!" : "FAILED!"); ?>
|