利用cookie统计访问网站次数
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN"
"http://www.w3.org/TR/REC-html140/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>Sample Page!</title>
<script LANGUAGE="JavaScript">
<!--
//一年有效时间
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 > 2) ? argv[2] : null;
var path = (argc > 3) ? argv[3] : null;
var domain = (argc > 4) ? argv[4] : null;
var secure = (argc > 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();
}
-->
</script>
</head>
<body>
<SCRIPT LANGUAGE="JavaScript">
var ncount=Count();
document.write("您好!" + UserName() + ". 您这是第 " + ncount + "次来本站!");
</SCRIPT>
<br>
<center>
<form onSubmit="return set();">
<input type="submit" value="重置">
</form>
</center>
</body>
</html>
当然这也不是很好的,因为设置cookie的时间失效就会清零了…………