重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
如何在ASP.NET MVC 3中对访问书进行统计?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。
成都创新互联公司是专业的遂川网站建设公司,遂川接单;提供成都网站建设、网站建设,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行遂川网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!string guid = Guid.NewGuid().ToString(); #region 生成js html.Append("function WriteCookie(cookieName, cookieValue, expiry) {"); //js写cookie html.Append(" var expDate = new Date();"); html.Append(" if (expiry) {"); html.Append(" expDate.setTime(expDate.getTime() + expiry);"); html.Append(" document.cookie = cookieName + \"=\" + escape(cookieValue) + \"; path=/;expires=\" + expDate.toGMTString();"); html.Append(" }"); html.Append(" else {"); html.Append(" document.cookie = cookieName + \"=\" + escape(cookieValue) + \"; path=/;\""); html.Append(" }"); html.Append("}"); html.Append("function ReadCookie(name) {"); //js读取cookie html.Append(" var cookieValue = \"\";"); html.Append(" var search = name + \"=\";"); html.Append(" if (document.cookie.length >0) {"); html.Append(" offset = document.cookie.indexOf(search);"); html.Append(" if (offset != -1) {"); html.Append(" offset += search.length;"); html.Append(" end = document.cookie.indexOf(\";\", offset);"); html.Append(" if (end == -1)"); html.Append(" end = document.cookie.length;"); html.Append(" cookieValue = unescape(document.cookie.substring(offset, end))"); html.Append(" }"); html.Append(" }"); html.Append(" return cookieValue;"); html.Append("}"); html.Append("DateFormatString = function(datetime, format) {"); html.Append(" var year = datetime.getFullYear();"); html.Append(" var month = datetime.getMonth()+1;"); html.Append(" var date = datetime.getDate();"); html.Append(" var hour = datetime.getHours();"); html.Append(" var minutes = datetime.getMinutes();"); html.Append(" var second = datetime.getSeconds();"); html.Append(" format = format.replace(/YY/g, year).replace(/MM/g, month).replace(/DD/g, date).replace(/hh/g, hour).replace(/mm/g, minutes).replace(/ss/g, second);"); html.Append(" return format;"); html.AppendLine("}"); html.Append("var cookieEnabled = (navigator.cookieEnabled) ? true : false;"); //判断浏览器是否支持cookie html.Append("if (typeof navigator.cookieEnabled == \"undefined\" &&!cookieEnabled) {"); html.Append(" document.cookie = \"testcookie\";"); html.Append(" cookieEnabled = (document.cookie == \"testcookie\") ? true : false;"); html.Append(" document.cookie = \"\";"); html.Append("}"); html.Append("var firstshow;"); html.Append("var visittotal;"); html.Append("var isLogin;"); html.Append("var loginName;"); html.Append("if (cookieEnabled == true) {"); html.Append(" var HBCountShowCookie = ReadCookie(\"HBCountShowCookie\");"); //访客浏览器ID html.Append(" var HBCountVisitTotalCookie = ReadCookie(\"HBCountVisitTotalCookie\");"); //访客浏览次数 html.Append(" var HBClientIDCookie = ReadCookie(\"HBClientIDCookie\");"); //访客上一个点击页面ID html.Append(" var HBLoginNameCookie = ReadCookie(\"HBLoginNameCookie\");"); //记录登录的用户名 html.Append(" if (HBCountShowCookie == \"\") {"); html.Append(" sparetime = 1000 * 60 * 60 * 24 * 3650;"); //有效期为一年 html.Append(" WriteCookie('HBCountShowCookie', \"" + guid + "\", sparetime);"); html.Append(" HBCountShowCookie = ReadCookie(\"HBCountShowCookie\");"); html.Append(" firstshow = 1;"); html.Append(" }"); html.Append(" else {"); html.Append(" firstshow = 0;"); html.Append(" }"); html.Append(" if (HBCountVisitTotalCookie == \"\") {"); //游客统计 html.Append(" sparetime = 1000 * 60 * 60 * 24 * 3650;"); html.Append(" WriteCookie('HBCountVisitTotalCookie', 1, sparetime);"); html.Append(" visittotal = 1;"); html.Append(" }"); html.Append(" else {"); html.Append(" if (firstshow == 1) {"); html.Append(" visittotal = parseInt(HBCountVisitTotalCookie) + 1;"); html.Append(" sparetime = 1000 * 60 * 60 * 24 * 3650;"); html.Append(" WriteCookie('HBCountVisitTotalCookie', visittotal, sparetime);"); html.Append(" }"); html.Append(" else {"); html.Append(" visittotal = parseInt(HBCountVisitTotalCookie);"); html.Append(" }"); html.Append(" }"); html.Append(" try {"); html.Append(" if (HBStat != undefined || HBStat.isLogin != undefined || HBStat.loginName != undefined) {"); //此为登录用户统计 html.Append(" if (HBStat.isLogin == 1 &&HBStat.loginName != \"\") {"); html.Append(" sparetime = 1000 * 60 * 60 * 24 * 120;"); html.Append(" WriteCookie('HBLoginNameCookie', HBStat.loginName, sparetime);"); html.Append(" HBLoginNameCookie = ReadCookie(\"HBLoginNameCookie\");"); html.Append(" }"); html.Append(" isLogin=HBStat.isLogin;"); html.Append(" }"); html.Append(" else{isLogin=0;}"); html.Append(" }"); html.Append(" catch (e) { isLogin=0; }"); html.Append(" loginName=HBLoginNameCookie;"); html.Append("} else {"); //不支持cookie html.Append(" firstshow = 0;"); html.Append(" visittotal = 1;"); html.Append(" var HBCountShowCookie = \"\";"); html.Append(" isLogin=0;"); html.Append(" loginName=\"\";"); html.Append("}"); html.Append("var ly = escape(document.referrer);"); html.Append("var currweb = escape(location.href);"); html.Append("var d = new Date();"); html.Append("var currdate=DateFormatString(d,'YY-MM-DD hh:mm:ss');"); html.Append("var screenwidth=screen.width;"); html.Append("var screenheight=screen.height;"); html.Append("var screencolordepth=screen.colorDepth;"); html.Append("document.write('');"); #endregion
生成之后再去执行下一个Action,用来写入一些统计数据,如客户端IP,浏览器信息,访问页面来源,当前页、访问时间、离开时间等!根据上一个访问页面地址、当前地址及上一个访问页面ID来判断是否是刷新操作还是新的访问页面,如果是刷新操作则写入刷新时间,否则写入上一个页面的离开时间,由于js的跨域问题,
if (assort.Equals(0)) { ly = Request.QueryString["Ly"]; //获取来源URL currWeb = Request.QueryString["CurrWeb"]; //获取当前URL } else { ly = Request.ServerVariables["Http_Referer"]; currWeb = ly; } string firstShow = Request.QueryString["FirstShow"]; //是否第一次访问 string visitTotal = Request.QueryString["VisitTotal"]; //获取浏览次数 string ip = Helper.Utils.ClientIP(); //获取客户端IP地址 string strAgent = Request.ServerVariables["HTTP_USER_AGENT"]; bool isAlexa = false; if (strAgent.IndexOf("alexa") >-1) //判断是否安装alexa工具栏 isAlexa = true; string browerName = Request.Browser.Browser; //浏览器名称 string browerVersion = Request.Browser.Version; //浏览器版本 string os = Helper.Utils.GetClientOS(); //客户端操作系统 string langage = Helper.Utils.GetLangage(); //客户端语言 string spider = Helper.Utils.GetSpiderBot(); //搜索引擎爬虫信息 bool isSpider = false; if (!string.IsNullOrEmpty(spider)) isSpider = true; HttpContext.Application.Lock(); ClickDataAccess clientBasic = new ClickDataAccess(); //刷新操作 需要根据上一个地址和当前地址 来判断 if (HttpContext.Application["HBCurrWeb_" + SiteID] != null &&HttpContext.Application["HBCurrWeb_" + SiteID].ToString().Equals(currWeb) &&HttpContext.Application["HBLy_" + SiteID] != null &&HttpContext.Application["HBLy_" + SiteID].ToString().Equals(ly) &&HttpContext.Application["HBClientId_" + SiteID] != null) { clientBasic.UpdateForRefresh(HttpContext.Application["HBClientId_" + SiteID]); //Helper.Utils.CreateFile("~/Log/", DateTime.Now.ToString("yyyyMMddHHmmssffff") + "_" + SiteID + "刷新.txt", writetext.ToString()); } else { if (HttpContext.Application["HBClientId_" + SiteID] != null) //如果存在上一个页面的ID值则写入离开时间 clientBasic.UpdateForLeaveTime(HttpContext.Application["HBClientId_" + SiteID]); ClickAndVisitorsDataAccess clientData = new ClickAndVisitorsDataAccess(); string[] outParam = clientData.Save(client); if (!string.IsNullOrEmpty(outParam[0])) HttpContext.Application["HBPublicId_" + SiteID] = outParam[0]; if (!string.IsNullOrEmpty(outParam[1])) { HttpContext.Application["HBClientId_" + SiteID] = outParam[1]; javaScript = "WriteCookie('HBClientIDCookie'," + int.Parse(outParam[1]) + ");"; } HttpContext.Application["HBCurrWeb_" + SiteID] = currWeb; HttpContext.Application["HBLy_" + SiteID] = ly; } HttpContext.Application.UnLock(); #endregion
关于如何在ASP.NET MVC 3中对访问书进行统计问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注创新互联行业资讯频道了解更多相关知识。