If have a web site on World Wide Web, you may be interested to know
how many users are online and how many numbers of users visited website. For
analyzing this thing you will go for log file on web server but that
information is usually difficult to read.
Please follow the steps to count No. of hits on web site or no. of
users online in asp.net using c#.
Step:-
1. Create new web site in VS 2008 or VS2010.
2. Add one Global.asax file.
3. Code for Global.asax.cs:-
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using
System.Web.UI.HtmlControls;
using
System.Web.UI.WebControls;
using
System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
/// <summary>
/// Summary
description for Global
/// </summary>
public class Global : System.Web.HttpApplication
{
private static
int totalNoOfUsers = 0;
private static
int currentNoOfUsers = 0;
/// <summary>
/// Required
designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
public Global()
{
InitializeComponent();
}
protected void
Application_Start(Object sender, EventArgs e)
{
int counterValue = 0;
string counterFile = Server.MapPath("~/Counter.txt");
if (File.Exists(counterFile))
{
using
(StreamReader sr = new
StreamReader(counterFile))
{
string
result = sr.ReadLine();
if
(result != null)
{
string
delimStr = "\t";
char[]
delimiter = delimStr.ToCharArray();
string[]
arrResult = null;
arrResult =
result.Split(delimiter);
if
(arrResult.Length == 2)
{
counterValue = Convert.ToInt32(arrResult[1]);
}
}
}
}
totalNoOfUsers = counterValue;
}
protected void
Session_Start(Object sender, EventArgs e)
{
totalNoOfUsers += 1;
currentNoOfUsers += 1;
string counterFile = Server.MapPath("~/Counter.txt");
if (File.Exists(counterFile))
{
//File.Delete(counterFile);
}
StreamWriter sw = File.CreateText(counterFile);
sw.WriteLine("Total\t" +
totalNoOfUsers);
sw.Close();
}
protected void
Application_BeginRequest(Object sender, EventArgs e)
{
}
protected void
Application_EndRequest(Object sender, EventArgs e)
{
}
protected void
Application_AuthenticateRequest(Object
sender, EventArgs e)
{
}
protected void
Application_Error(Object sender, EventArgs e)
{
}
protected void
Session_End(Object sender, EventArgs e)
{
currentNoOfUsers -= 1;
}
protected void
Application_End(Object sender, EventArgs e)
{
}
public static
int TotalNumberOfUsers
{
get
{
return
totalNoOfUsers;
}
}
public static
int CurrentNumberOfUsers
{
get
{
return
currentNoOfUsers;
}
}
#region Web Form Designer generated code
/// <summary>
/// Required
method for Designer support - do not modify
/// the
contents of this method with the code editor.
/// </summary>
private void
InitializeComponent()
{
this.components = new System.ComponentModel.Container();
}
#endregion
}
<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="Default.aspx.cs"
Inherits="_Default"
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD
XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Untitled Page</title>
</head>
<body>
<form
id="form1"
runat="server">
<div>
<asp:TextBox ID="TextBox1"
runat="server"></asp:TextBox>
<asp:Button ID="Button1"
runat="server"
Text="Button"
onclick="Button1_Click"
/>
Total No of Users:- <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
No. Of Users Online :- <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
</div>
</form>
</body>
</html>
5. Default.aspx.cs:-
protected void Page_Load(object sender, EventArgs e)
{
int currentNumberOfUsers = Global.CurrentNumberOfUsers;
int totalNumberOfUsers = Global.TotalNumberOfUsers;
Label2.Text = currentNumberOfUsers.ToString();
Label1.Text = totalNumberOfUsers.ToString();
}
6. Create one text file in root folder with
a name Counter.txt.
Run the code.
No comments:
Post a Comment
Note: only a member of this blog may post a comment.