统计网站访问量,同一IP在网页有效期内计数一次,跪求代码!

mozhemin 2012-12-11 02:55:10
统计网站访问量,同一IP在网页有效期内计数一次,跪求代码!
小弟是菜鸟。。。可以给个直接复制粘贴的代码吗?
我找了一个但是一刷新就+1
代码如下:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script language="JavaScript">
<!--
var caution = false
function setCookie(name, value, expires, path, domain, secure) {
var curCookie = name + "=" + escape(value) +
((expires) ? "; expires=" + expires.toGMTString() : "") +
((path) ? "; path=" + path : "") +
((domain) ? "; domain=" + domain : "") +
((secure) ? "; secure" : "")
if (!caution || (name + "=" + escape(value)).length <= 4000)
document.cookie = curCookie
else
if (confirm("Cookie exceeds 4KB and will be cut!"))
document.cookie = curCookie
}
function getCookie(name) {
var prefix = name + "="
var cookieStartIndex = document.cookie.indexOf(prefix)
if (cookieStartIndex == -1)
return null
var cookieEndIndex = document.cookie.indexOf(";", cookieStartIndex + prefix.length)
if (cookieEndIndex == -1)
cookieEndIndex = document.cookie.length
return unescape(document.cookie.substring(cookieStartIndex + prefix.length, cookieEndIndex))
}
function deleteCookie(name, path, domain) {
if (getCookie(name)) {
document.cookie = name + "=" +
((path) ? "; path=" + path : "") +
((domain) ? "; domain=" + domain : "") +
"; expires=Thu, 01-Jan-70 00:00:01 GMT"
}
}
function fixDate(date) {
var base = new Date(0)
var skew = base.getTime()
if (skew > 0)
date.setTime(date.getTime() - skew)
}
var now = new Date()
fixDate(now)
now.setTime(now.getTime() + 365 * 24 * 60 * 60 * 1000)
var visits = getCookie("counter")
if (!visits)
visits = 1
else
visits = parseInt(visits) + 1
setCookie("counter", visits, now)
document.write("<center>您是第 <font color='red' size='5'>" + visits + " </font>个来到本站访客</center>")
// -->
</script>

</head>

<body>
</body>
</html>
...全文
517 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
zcxverygood123456 2012-12-12
  • 打赏
  • 举报
回复
用数据库记录嘛,访问之前判断比较,如果第一次访问,就统计就+1
洳夢绚染 2012-12-12
  • 打赏
  • 举报
回复
数据库记录访问过得ip 之后每次增加访问量之前进行判断
爱在夕阳下 2012-12-12
  • 打赏
  • 举报
回复
用 Session.SessionID 判断是否是同一个用户请求
jeven_xiao 2012-12-12
  • 打赏
  • 举报
回复
如果是你网站的正常会员的话在数据库中记录吧 如果是访客的话在cookie中记录
nickppa 2012-12-12
  • 打赏
  • 举报
回复
在服务器端取得ip,下面这段代码是我网上找的
public static string GetIP(Page paramPage)
	{
		string strIP = string.Empty;
		if(paramPage.Request.ServerVariables["HTTP_VIA"] != null && paramPage.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != null) // 服务器, using proxy
		{
			strIP = CBase.TransToTrimString(paramPage.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]);  // Return real client IP.
		}
		else//如果没有使用代理服务器或者得不到客户端的ip  not using proxy or can't get the Client IP
		{
			strIP = CBase.TransToTrimString(paramPage.Request.ServerVariables["REMOTE_ADDR"]); //While it can't get the Client IP, it will return proxy IP.
		}
		return strIP;
	}
然后你把ip,日期记录到数据库中 数据库设计,2种,第二种合理一点 第一种简单一点(数据量会大):ip、日期(每次插入前先查看相同的ip、日期是否存在,如果不存在就插入,统计时只要count(0)) 第二种复杂点(多少个ip访问过就多少条记录):ip、更新日期、计数(每次更新时对当前日期跟更新日期作比较,不是同一天的计数就+1,不存在的就插入一条新的,统计时要sum(计数))
mozhemin 2012-12-11
  • 打赏
  • 举报
回复
就是要个不会一刷新,统计就+1的,要同个IP一天只记录一次
Assassin_ 2012-12-11
  • 打赏
  • 举报
回复
personball 2012-12-11
  • 打赏
  • 举报
回复
啥叫“网页有效期”
mozhemin 2012-12-11
  • 打赏
  • 举报
回复

62,072

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧