使用cookie实现统计访问者登陆次数

yipeiwu_com5年前PHP代码库
复制代码 代码如下:

<?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支持断点续传、分块下载的类

本文是为大家分享php支持断点续传、分块下载的类,供大家参考,具体内容如下 <?php /** * User: djunny * Date: 2016-04-2...

php foreach如何跳出两层循环(详解)

使用break可以跳出当前循环,那如果想再跳出上一层的循环呢 我们就需要break 2即可 $arr1 = array('a1','a2','a3','a4');...

PHP实现的文件操作类及文件下载功能示例

本文实例讲述了PHP实现的文件操作类及文件下载功能。分享给大家供大家参考,具体如下: 文件操作类: <?php // Copyright 2005, Lee Babi...

逆序二维数组插入一元素的php代码

复制代码 代码如下: <?php /** * 逆序二维数组插入一元素 * * @author WadeYu * @date 2012-05-30 */ $aSorted = arr...

php中strstr、strrchr、substr、stristr四个函数的区别总结

php中strstr、strrchr、substr、stristr四个函数用法区别: php中strstr strrchr substr stristr这四个字符串操作函数特别让人容易混...