---
title: "利用cookie统计访问网站次数"
author: "GoodWell"
date: "2011-09-05T13:06:56Z"
tags: []
categories: ["Web前端"]
slug: "cookie"
draft: false
lastmod: "2011-09-05T13:06:56Z"
id: 1165732
url: "https://goodwell.iteye.com/blog/1165732"
---

&lt;! DOCTYPE HTML PUBLIC　"-//W3C//DTD HTML 4.0//EN"  
"[http://www.w3.org/TR/REC-html140/strict.dtd](http://www.w3.org/TR/REC-html140/strict.dtd)"&gt;  
&lt;html&gt;  
&lt;head&gt;  
&lt;meta http-equiv="Content-Type" content="text/html; charset=gb2312"&gt;  
&lt;title&gt;Sample Page!&lt;/title&gt;  
&lt;script LANGUAGE="JavaScript"&gt;  
&lt;!--  
//一年有效时间  
var expDays = 365;   
var exp = new Date();  
exp.setTime(exp.getTime() + (expDays\*24\*60\*60\*1000));  
//查询用户名称  
function UserName(info)  
{  
   var VisitorName = GetCookie('VisitorName')  
   if (VisitorName == null)  
   {  
     VisitorName = prompt("Input your UserName:");  
     SetCookie ("VisitorName", VisitorName, exp);  
   }  
   return VisitorName;  
}  
//计数函数  
function Count(info)  
{  
   var VisitCount = GetCookie('VisitCount')  
   if (VisitCount == null)  
   {  
      VisitCount = 0;  
   }  
   else  
   {  
      VisitCount++;  
   }  
   SetCookie ('VisitCount', VisitCount, exp);  
   return VisitCount;  
}  
//重置函数  
function set()  
{    
   DeleteCookie('VisitorName');  
   DeleteCookie('VisitCount');  
   VisitorName = prompt("who are you?");  
   SetCookie ('VisitorName', VisitorName, exp);  
   SetCookie ('VisitCount', 0, exp);  
}  
//读取cookie  
function GetCookie(sName)  
{     
   var RegularExp = "(?:; )?" + sName + "=(\[^;]\*);?";  
   var regExp = new RegExp(RegularExp);  
   if (regExp.test(document.cookie))  
   {  
     return RegExp\["$1"];  
   }  
   else  
   {  
     return null;  
   }  
}  
//建立cookie  
function SetCookie (name, value)  
{    
   var argv = SetCookie.arguments;  
   var argc = SetCookie.arguments.length;  
   var expires = (argc &gt; 2) ? argv\[2] : null;  
   var path = (argc &gt; 3) ? argv\[3] : null;  
   var domain = (argc &gt; 4) ? argv\[4] : null;  
   var secure = (argc &gt; 5) ? argv\[5] : false;

   document.cookie = name + "=" + escape (value) +  
     ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +  
     ((path == null) ? "" : ("; path=" + path)) +  
     ((domain == null) ? "" : ("; domain=" + domain)) +  
     ((secure == true) ? "; secure" : "");  
}  
 //删除cookie  
function DeleteCookie (name)  
{  
   var exp = new Date();  
   exp.setTime (exp.getTime() - 1);  
   var CookieName = GetCookie (name);  
   document.cookie = name + "=" + CookieName + "; expires=" + exp.toGMTString();  
}  
\--&gt;  
&lt;/script&gt;  
&lt;/head&gt;  
&lt;body&gt;  
&lt;SCRIPT LANGUAGE="JavaScript"&gt;

var ncount=Count();  
document.write("您好!" + UserName() + ". 您这是第 " + ncount + "次来本站！");

&lt;/SCRIPT&gt;

&lt;br&gt;  
&lt;center&gt;  
&lt;form onSubmit="return  set();"&gt;    　  
   &lt;input type="submit" value="重置"&gt;  
&lt;/form&gt;  
&lt;/center&gt;  
&lt;/body&gt;  
&lt;/html&gt;

当然这也不是很好的，因为设置cookie的时间失效就会清零了…………