使用cookie实现统计访问者登陆次数
程序员文章站
2023-04-07 17:48:26
复制代码 代码如下:
<?php
$_cookie["counter"]?($c=$_cookie["counter"]+1):($c=1);
setcookie("counter",$c,time()+60);
echo "<b>欢迎您第"."<font color=#ff0000>".$c."</font>次访问cookie</b>";
?>
在这个应用程序中,首先是浏览器请求一个资源(这个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["counter"]?($c=$_cookie["counter"]+1):($c=1);
setcookie("counter",$c,time()+60);
echo "<b>欢迎您第"."<font color=#ff0000>".$c."</font>次访问cookie</b>";
?>
在这个应用程序中,首先是浏览器请求一个资源(这个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 常用类汇总 推荐收藏