Cấp bậc tác giả:

DOTNET

Thống kê truy cập website ASP.NET

Được viết bởi QuangIT ngày 31/07/2012 lúc 02:42 PM
Bạn cần thống kê theo dõi xem có bao nhiêu khách đang truy cập website và đã có bao nhiêu lượt truy cập website của bạn. Mình sẽ hướng dẫn bạn làm việc đó.
  • 0
  • 14064
Tải tệp tin: Click ở đây

Thống kê truy cập website ASP.NET

Bạn cần thống kê theo dõi xem có bao nhiêu khách đang truy cập website và đã có bao nhiêu lượt truy cập website của bạn. Mình sẽ hướng dẫn bạn làm việc đó.

Trước tiên mình nói qua về Chu kỳ sống của trang web trong ASP.NET (ASP.NET Page Life Cycle) Chu kỳ sống được bắt đầu khi trình duyệt yêu cầu một trang web gọi là Session.

Chu kỳ sống vẫn tiếp tục nếu:
- Session đang hoạt động.
- Người sử dụng tương tác với giao diện web cho đến khi kích hoạt một sự kiện.
- Dữ liệu của trang (View State) wed được gửi về cho Server.
- Server nhận được View State và trả lại yêu cầu từ View State.
Chu kỳ sống kết thúc khi:
- Người dùng kết thúc trình duyệt.
- Session kết thúc (timeout).
Mỗi khi người dùng duyệt web của mình sẽ có các sự kiện ứng dụng sau:
Application_Start: Người dùng đầu tiên duyệt trang web.
Application_End: Khi không còn người dùng nào duyệt trang web.
Application_Error: Khi có lỗi xảy ra trong ứng dụng
Session_Start: Khi người dùng duyệt một trang web
Session_End: Khi người dùng đóng trình duyệt hoặc Session kết thúc (time out)
Các sự kiện ứng dụng này được “khởi tạo” trong Global.asax, Bạn hãy tạo file Global.asax trong thư mục webroot của bạn và nhập nội dung file như sau:
View Code:
<%@ Application Language="C#" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Web" %>


<script runat="server">

void Application_Start(object sender, EventArgs e) 
{
// Code that runs on application startup
string path = Server.MapPath("~") + "\\count.txt";
if (!File.Exists(path))
File.WriteAllText(path, "0");

Application["Access"] = int.Parse(File.ReadAllText(path)); 

}

void Application_End(object sender, EventArgs e) 
{
// Code that runs on application shutdown
string path = Server.MapPath("~") + "\\count.txt";

File.WriteAllText(path, Application["Access"].ToString());
}

void Application_Error(object sender, EventArgs e) 

// Code that runs when an unhandled error occurs

}

void Session_Start(object sender, EventArgs e) 
{
string path = Server.MapPath("~") + "\\count.txt";
// Code that runs when a new session is started
if (Application["Online"] == null)
Application["Online"] = 1;
else
Application["Online"] = (int)Application["Online"] + 1;

Application["Access"] = (int)Application["Access"] + 1;
File.WriteAllText(path, Application["Access"].ToString());
}

void Session_End(object sender, EventArgs e) 
{
// Code that runs when a session ends. 
// Note: The Session_End event is raised only when the sessionstate mode
// is set to InProc in the Web.config file. If session mode is set to StateServer 
// or SQLServer, the event is not raised.
int i = (int)Application["Online"];
if (i > 1)
Application["Online"] = i - 1;

string path = Server.MapPath("~") + "\\count.txt";
File.WriteAllText(path, Application["Access"].ToString()); 
}

</script>

Thêm vào Default.aspx
View Code:
Tổng số: 
<asp:Label ID="Label1" runat="server" Text=""><%= Application["Access"].ToString()%></asp:Label> lượt truy cập

View Code:
Đang Online: 
<asp:Label ID="lblCount" runat="server" Text=""><%= Application["Online"].ToString()%></asp:Label> đang online

Nguồn bài viết: Dngaz.com

BÌNH LUẬN BÀI VIẾT

Bài viết mới nhất

LIKE BOX

Bài viết được xem nhiều nhất

HỌC HTML