>  기사  >  백엔드 개발  >  php使用cookie统计访问者登陆次数的代码举例

php使用cookie统计访问者登陆次数的代码举例

WBOY
WBOY원래의
2016-07-25 08:59:03824검색
  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.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

有了这样的分析,希望有助于大家理解与掌握php中cookie的用法。



성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.