複製代碼 代碼如下:
$_COOKIE["counter"]?($c=$_COOKIE["counter"]+1):($c=1);
setCookie("counter",$c,time()+60);
echo "
歡迎您第"."".$c."次訪問cookie";
?>
在這個應用程式中,首先是瀏覽器請求一個資源(這個php頁面) ,發送下面的HTTP包頭內容到伺服器:
GET http://localhost/index.php HTTP/1.1
HOST:localhost
Accept:*/*
Accept-language:zh-cn
Accept-Encoding:gzip,deflate
User-Agent:Mozilla/4.0 (compatible;MSIE 6.0;Windows NT 5.1;SV1)
Connection:Keep-Alive
---------------------------------------------------------------------------
現在是動態網頁程式(index.php)建立了Cookie,那麼,伺服器會傳輸下面的HTTP前序內容到瀏覽器:
HTTP/1.1 200 OK
Server:Apache/2.2.6 (Win32) PHP/5.2.6
Date:Fri,23 Mar 2009 23:15:55 GMT
Connection:Keep-Alive
Content-Length:65
Content-Typt:text/html
Set-Cookie:VisitorCount=1; expires=Thr,30-Jul-2010 16:00:00 GMT;domain=localhost;path=/
Cache-control:private
GET http://localhost/index.php HTTP/1.1
---------------------------------------------------------------------------
這將在用戶端儲存一個cookie檔案,並儲存$c變數
當再次請求時,就會將cookie中的資料傳給伺服器,例如下邊的HTTP請求前序:
Accept:*/*
Accept-language:zh-cn
Pragma:no-cache
User-Agent:Mozilla/4.0(compatible;MSIE 6.0;Windows NT 5.1; SV1)
Host:localhost
Connection:Keep-Alive
Cookie:VisitorCount=1
http://www.bkjia.com/PHPjc/327476.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/327476.htmlTechArticle複製代碼 代碼如下: ?php $_COOKIE["counter"]?($c=$_COOKIE["counter"]+1):($c=1); setCookie("counter",$c,time()+60); echo "b歡迎您第"."font color=#ff0000".$c."/font次訪問...