Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement an access Statistics system with ASP.NET MVC 3

2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article shows you how to use ASP.NET MVC 3 to achieve an access statistics system, the content is concise and easy to understand, absolutely can make your eyes bright, through the detailed introduction of this article, I hope you can get something.

To run a website, we often need to analyze users' behavior, users' habits, which part of the site users value, which part of the site is useful to users, and where the information comes from. At this point, we need to access the statistical system. There are already a lot of statistical systems on the Internet, such as webmaster statistics, Baidu statistics, Google analytics and so on, other people's things are always someone else's, why don't we realize the statistical function ourselves, and what we write can achieve some special functions, such as login, order behavior, can better integrate their own system!

Let's use ASP.NET MVC 3 to implement an access statistics system! First, use the program to generate a piece of js code, including reading and writing Cookie, and writing a unique value to Cookie, which is used to judge the unique visitor, and then write the number of visits and the ID value of the last visited page; then it is to get the client time, resolution and so on.

String guid = Guid.NewGuid (). ToString (); # region generates js html.Append ("function WriteCookie (cookieName, cookieValue, expiry) {"); / / js writes cookie html.Append ("var expDate = new Date ();"); html.Append ("if (expiry) {"); html.Append ("expDate.setTime (expDate.getTime () + expiry);"); html.Append ("[xss_clean] = cookieName +\" =\ "+ escape (cookieValue) +\"; Expires=\ "+ expDate.toGMTString ();"; html.Append ("}"); html.Append ("else {"); html.Append ("[xss_clean] = cookieName +\" =\ "+ escape (cookieValue) +\"; path=/;\ "); html.Append ("} "); html.Append ("} "); html.Append (" function ReadCookie (name) {"); / / js read cookie html.Append (" var cookieValue =\ "\"; ") Html.Append ("var search = name +\" =\ ";"); html.Append ("if ([xss_clean]. Length > 0) {"); html.Append ("offset = [xss_clean] .indexOf (search);"); html.Append ("if (offset! =-1) {"); html.Append ("offset + = search.length;"); html.Append ("end = [xss_clean] .indexOf (\";\ ", offset);") Html.Append ("if (end =-1)"); html.Append ("end = [xss_clean] .length;"); html.Append ("cookieValue = unescape ([xss_clean] .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; "); / / determine whether the browser supports cookie html.Append (" if (typeof navigator.cookieEnabled =\ "undefined\" & &! cookieEnabled) {"); html.Append (" [xss_clean] =\ "testcookie\"; "); html.Append (" cookieEnabled = ([xss_clean] = =\ "testcookie\")? True: false; "); html.Append (" [xss_clean] =\ "\"; "); 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\"); ") / / unique ID html.Append of the guest browser ("var HBCountVisitTotalCookie = ReadCookie (\" HBCountVisitTotalCookie\ "); / / number of visits to the visitor html.Append (" var HBClientIDCookie = ReadCookie (\ "HBClientIDCookie\"); / / the last click on the page by the visitor ID html.Append ("var HBLoginNameCookie = ReadCookie (\" HBLoginNameCookie\ ");"); / / record the login user name html.Append ("if (HBCountShowCookie =\") {") Html.Append ("sparetime = 1000 * 60 * 24 * 3650;"); / / valid for one year 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 = =\") {"); / / tourist statistics 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 * 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) {"); / / this is the login user statistics 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 (dGramley YYMurMmurDD hh:mm:ss');"); html.Append ("var screenwidth=screen.width;"); html.Append ("var screenheight=screen.height;"); html.Append ("var screencolordepth=screen.colorDepth;"); html.Append ("[xss_clean] (');"); # endregion

Generate and then execute the next Action, which is used to write some statistics, such as client IP, browser information, access page source, current page, access time, departure time, etc.! Determine whether it is a refresh operation or a new access page according to the last access page address, the current address and the previous access page ID. If it is a refresh operation, write the refresh time, otherwise write the departure time of the previous page, due to the cross-domain problem of js

If (assort.Equals (0)) {ly = Request.QueryString ["Ly"]; / / get the source URL currWeb = Request.QueryString ["CurrWeb"]; / / get the current URL} else {ly = Request.ServerVariables ["Http_Referer"]; currWeb = ly;} string firstShow = Request.QueryString ["FirstShow"]; / / whether it is the first time to visit string visitTotal = Request.QueryString ["VisitTotal"] / / get the number of views string ip = Helper.Utils.ClientIP (); / / get the client IP address string strAgent = Request.ServerVariables ["HTTP_USER_AGENT"]; bool isAlexa = false; if (strAgent.IndexOf ("alexa") >-1) / / determine whether to install the alexa toolbar isAlexa = true; string browerName = Request.Browser.Browser; / / browser name string browerVersion = Request.Browser.Version / / browser version string os = Helper.Utils.GetClientOS (); / / client operating system string langage = Helper.Utils.GetLangage (); / / client language string spider = Helper.Utils.GetSpiderBot (); / / search engine crawler information bool isSpider = false; if (! string.IsNullOrEmpty (spider)) isSpider = true; HttpContext.Application.Lock (); ClickDataAccess clientBasic = new ClickDataAccess () / / refresh operation needs to judge 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 + "refresh .txt", writetext.ToString ();} else {if (HttpContext.Application ["HBClientId_" + SiteID]! = null) / / if the ID value of the previous page exists, write the departure time 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

Application is used here to store objects, but you can also use cache classes to do so.

For the time being, there is no solution to the departure time when leaving the current website, of course, if the domain name of the access statistics system and the domain name of the website being counted are the same top-level domain name, you can do this!

The above content is how to use ASP.NET MVC 3 to implement an access statistics system, have you learned the knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report