|          
Cookie Check--------------------------------------------------------------------------------
 HTTP 相关函式库
 --------------------------------------------------------------------------------
 
 
 
 本函式库共有 2 个函式
 header: 送出 HTTP 协定的标头到浏览器
 setcookie: 送出 Cookie 资讯到浏览器。
 
 
 --------------------------------------------------------------------------------
 函式:header()
 --------------------------------------------------------------------------------
 
 
 
 HTTP 相关函式库
 
 
 header
 送出 HTTP 协定的标头到浏览器
 
 语法: int header(string string);
 
 传回值: 整数
 
 函式种类: 网路系统
 
 
 
 
 内容说明
 
 
 标头 (header) 是伺服器以 HTTP 协定传 HTML 资料到浏览器前所送出的字串,在标头与 HTML 文件之间尚需空一行分隔。有关 HTTP 的详细说明,可以参考坊间的相关书籍或更详细的 RFC 2068 官方文件(http://www.w3.org/Protocols/rfc2068/rfc2068)。在 PHP 中送回 HTML 资料前,需先传完所有的标头。
 
 注意: 传统的标头一定包含下面三种标头之一,并只能出现一次。
 
 Content-Type: xxxx/yyyy
 Location: xxxx:yyyy/zzzz
 Status: nnn xxxxxx
 在新的多型标头规格 (Multipart MIME) 方可以出现二次以上。
 
 
 
 
 使用范例
 
 
 范例一: 本例用来重导使用者到 PHP 的官方网站。
 <>
 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");
 
 范例三: 让使用者的浏览器出现找不到档案的讯息。
 <>
 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 相关函式库
 
 
 setcookie
 送出 Cookie 资讯到浏览器。
 
 语法: int setcookie(string name, string value, int expire, string path, string domain, int secure);
 
 传回值: 整数
 
 函式种类: 网路系统
 
 
 
 
 内容说明
 
 
 本函式会跟着标头 Header 送出一段小资讯字串到浏览器。使用本函式要在送出 HTML 资料前,实际上 cookie 也算标头的一部份。本函式的参数除了第一个 name 之外,都是可以省略的。参数 name 表示 cookie 的名称;value 表示这个 cookie 的值,这个参数为空字串则表示取消浏览器中该 cookie 的资料;expire 表示该 cookie 的有效时间;path 为该 cookie 的相关路径;domain 表示 cookie 的网站;secure 则需在 https 的安全传输时才有效。想得到更多的 cookie 资讯可以到 http://www.netscape.com/newsref/std/cookie_spec.html,由 cookie 原创者 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 Status:
 <>
 printf ('%s
 ;',
 $status ? "00FF00" : "FF0000",
 $status ? "PASSED!" : "FAILED!");
 ?>
 
 
 
 |