php使用cookie統計訪問者登陸次數的代碼舉例

來源:互聯網
上載者:User
  1. $_COOKIE["counter"]?($c=$_COOKIE["counter"]+1):($c=1);
  2. setCookie("counter",$c,time()+60);
  3. echo "歡迎您第"."".$c."次訪問cookie";
  4. //by bbs.it-home.org
  5. ?>
複製代碼

下面是有關這個例子的代碼說明,供大家參考。

以上代碼中,首先,瀏覽器請求一個資源(這個php頁面),發送下面的HTTP包頭內容到伺服器:

GET http://localhost/index.php HTTP/1.1HOST:localhostAccept:*/*Accept-language:zh-cnAccept-Encoding:gzip,deflateUser-Agent:Mozilla/4.0 (compatible;MSIE 6.0;Windows NT 5.1;SV1)Connection:Keep-Alive

**頁程式(index.php)建立了Cookie,伺服器傳輸下面的HTTP前序內容到瀏覽器:

HTTP/1.1 200 OKServer:Apache/2.2.6 (Win32) PHP/5.2.6Date:Fri,23 Mar 2009 23:15:55 GMTConnection:Keep-AliveContent-Length:65Content-Typt:text/htmlSet-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-cnPragma:no-cacheUser-Agent:Mozilla/4.0(compatible;MSIE 6.0;Windows NT 5.1; SV1)Host:localhostConnection:Keep-AliveCookie:VisitorCount=1

有了這樣的分析,希望有助於大家理解與掌握php中cookie的用法。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.