Today, I added a simple website statistics on my website:
You can move your mouse over it to display the following simple website statistics.
Now let's talk about how it is implemented. The Asp.net 2003 version I used.
The total number of articles. The total number of files is the operation to read the database.
For the total access traffic, I saved this parameter in the count. xml file and obtained it by reading the modification of this file. For the online access traffic, I set the parameters through the Application in global.
The count. XML Code is as follows (simple ):
<? XML version = "1.0" standalone = "yes"?>
<Mycount>
<Totalcount> 7 </totalcount>
</Mycount>
The key code in global is as follows:
Protected void application_start (Object sender, eventargs E)
...{
Dataset objdataset = new dataset ();
Objdataset. readxml (server. mappath ("count. xml "));
Int maxcount = convert. toint32 (objdataset. Tables ["mycount"]. Rows [0] ["totalcount"]);
Application ["totalcount"] = maxcount;
Application ["onlinecount"] = 0;
}
Protected void session_start (Object sender, eventargs E)
...{
Application. Lock ();
Application ["onlinecount"] = (INT) application ["onlinecount"] + 1;
// Add 1 to the total number of visits
Int COUNT = 0;
Count = (INT) application ["totalcount"];
Count = count + 1;
Application ["totalcount"] = count;
// Write data records to files
Dataset objdataset = new dataset ();
Objdataset. readxml (server. mappath ("count. xml "));
Objdataset. Tables ["mycount"]. Rows [0] ["totalcount"] = (INT) application ["totalcount"];
Objdataset. writexml (server. mappath ("count. xml "));
Application. Unlock ();
}
Protected void session_end (Object sender, eventargs E)
...{
Application. Lock ();
Application ["onlinecount"] = (INT) application ["onlinecount"]-1;
Application. Unlock ();
}
Protected void application_end (Object sender, eventargs E)
...{
Int COUNT = 0;
Count = (INT) application ["totalcount"];
Dataset objdataset = new dataset ();
Objdataset. readxml (server. mappath ("count. xml "));
Objdataset. Tables ["mycount"]. Rows [0] ["totalcount"] = (INT) application ["totalcount"];
Objdataset. writexml (server. mappath ("count. xml "));
}
In the displayed part of the code, I wrote it in the user control, which is highly reusable. The Code is as follows:
<A class = "" href = '# 'title =' total number of articles: <% = articlecount %>
Total number of files: <% = filecount %>
Total visits: <% = totalcount %>
Online access volume: <% = onlinecount %> '>
Website Statistics </a>
. CS code
Private void page_load (Object sender, system. eventargs E)
...{
// Place user code here to initialize the page
If (! Page. ispostback)
...{
If (application ["onlinecount"]! = NULL)
...{
// Obtain the current number of online users
Onlinecount = application ["onlinecount"]. tostring ();
}
If (application ["totalcount"]! = NULL)
...{
// Obtain the total number of visitors to the website
Totalcount = application ["totalcount"]. tostring ();
}
This. articlecount = bll_article.countarticle ("1"). tostring ();
This. filecount = bll_file.countfile ("1"). tostring ();
}
}
Of course, this is a very simple statistics. If you need to count the access situation this week or this month, you can add some parameters in count. XML to continue the operation.
Note that the application parameter can also be replaced by the session parameter. The usage is the same.