各位大侠:
本人遇到工作难题!不知道.net的在线统计怎么下手.就是统计当前在线的会员数目,每5分钟统计一回,不知道各位大哥有没有做过的或者看到过这方面的资料!会员的登陆方式采用Cookie进行标识的!如果也能统计不是会员的数目更好!
本人倾仅有100分求解决该问题的方式,有资料,或者代码都行!
先谢了!
本人遇到工作难题!不知道.net的在线统计怎么下手.就是统计当前在线的会员数目,每5分钟统计一回,不知道各位大哥有没有做过的或者看到过这方面的资料!会员的登陆方式采用Cookie进行标识的!如果也能统计不是会员的数目更好!
本人倾仅有100分求解决该问题的方式,有资料,或者代码都行!
先谢了!
解决方案 »
- swfupload upload error 500
- UpdatePanel 提示框问题!?
- JS小问题
- DataGrid绑定列不能获取修改后的文本框数据
- URL重写所显示的页面,刷新后暴露了真正地址,有没办法可以把该地址隐藏起来?
- 如何读取 oracle 中blob的内容
- 我不懂C#,有没有能帮我把这段代码转成VB呀?急用的,在线等
- Asp.Net应用程序中,怎样配置数据库连接,可以使开发环境到应用环境迁移数据库时不修改程序。
- 大家推荐几个美观大方的企业模板或页面,由于没有美工,自已做出来的东西拿不出手
- databound 问题?
- 如何将html文件转换为jpg等图片的文件
- 在线统计asp.net
------说错了莫怪。
新建一个文件global.asax并添加如下代码
<script Language="C#" runat="server"> void Session_Onstart()
{
Application.Lock();
Application["whoson"] = Convert.ToInt32(Application["whoson"]) + 1;
Application.UnLock();
}
void Session_OnEnd()
{
Application.Lock();
Application["whoson"] = Convert.ToInt32(Application["whoson"]) - 1;
Application.UnLock();
}
public void Application_OnStart(){
Application.Lock();
Application["whoson"]=0;
Application.UnLock();
}</script>
------说错了莫怪。
------说错了莫怪。
using System;
using System.Collections;
using System.ComponentModel;
using System.Web;
using System.Web.SessionState;
using System.Data.SqlClient;
namespace _016Count_People
{
/// <summary>
/// Global 的摘要说明。
/// </summary>
public class Global : System.Web.HttpApplication
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null; public Global()
{
InitializeComponent();
}
protected void Application_Start(Object sender, EventArgs e)
{
SqlConnection con=new SqlConnection("server=.;database=countpeople;uid=sa;pwd=cyx7688");
con.Open();
SqlCommand cmd=new SqlCommand("select * from countpeople",con);
int count=Convert.ToInt32(cmd.ExecuteScalar());
con.Close();
Application["totol"]=count;
Application["online"]=0;
}
protected void Session_Start(Object sender, EventArgs e)
{
Session.Timeout=1;
Application.Lock();
Application["totol"]=(int)Application["totol"]+1;
Application["online"]=(int)Application["online"]+1;
Application.UnLock(); } 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)
{
Application.Lock();
Application["online"]=(int)Application["online"]-1;
Application.UnLock();
} protected void Application_End(Object sender, EventArgs e)
{
SqlConnection con=new SqlConnection("server=.;database=countpeople;uid=sa;pwd=cyx7688");
con.Open();
SqlCommand cmd=new SqlCommand("update countpeople set num="+Application["totol"].ToString(),con);
cmd.ExecuteNonQuery();
con.Close();
}
#region Web 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
}
#endregion
}
}
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
namespace _016Count_People
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Label lblOnLine;
protected System.Web.UI.WebControls.Label lblTotol;
protected System.Web.UI.WebControls.Label Label2;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
this.lblTotol.Text=Application["totol"].ToString();
this.lblOnLine.Text=Application["online"].ToString();
string userName=Request.Form["userName"].ToString();
string userPwd=Request.Form["userPwd"].ToString();
SqlConnection con=new SqlConnection("server=.;database=Login;uid=sa;pwd=cyx7688");
con.Open();
SqlCommand cmd=new SqlCommand("select Count(*) from login where userName='"+userName+"' and userPwd='"+userPwd+"'",con);
int count=Convert.ToInt32(cmd.ExecuteScalar());
if(count>0)
{
Session["flag"]=true;
Response.Redirect("main.aspx?userName='"+userName);
}
else
{
Response.Redirect("loginfail.htm");
}
con.Close();
} #region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load); }
#endregion }
}
using System;
using System.Collections;
using System.ComponentModel;
using System.Web;
using System.Web.SessionState;
using System.Data.SqlClient;
namespace _016Count_People
{
/// <summary>
/// Global 的摘要说明。
/// </summary>
public class Global : System.Web.HttpApplication
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null; public Global()
{
InitializeComponent();
}
protected void Application_Start(Object sender, EventArgs e)
{
SqlConnection con=new SqlConnection("server=.;database=countpeople;uid=sa;pwd=cyx7688");
con.Open();
SqlCommand cmd=new SqlCommand("select * from countpeople",con);
int count=Convert.ToInt32(cmd.ExecuteScalar());
con.Close();
Application["totol"]=count;
Application["online"]=0;
}
protected void Session_Start(Object sender, EventArgs e)
{
Session.Timeout=1;
Application.Lock();
Application["totol"]=(int)Application["totol"]+1;
Application["online"]=(int)Application["online"]+1;
Application.UnLock(); } 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)
{
Application.Lock();
Application["online"]=(int)Application["online"]-1;
Application.UnLock();
} protected void Application_End(Object sender, EventArgs e)
{
SqlConnection con=new SqlConnection("server=.;database=countpeople;uid=sa;pwd=cyx7688");
con.Open();
SqlCommand cmd=new SqlCommand("update countpeople set num="+Application["totol"].ToString(),con);
cmd.ExecuteNonQuery();
con.Close();
}
#region Web 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
}
#endregion
}
}
然后建立一个线程,每五分钟轮询一次,然后使用ajax的“推”技术发给管理员就的了
{
// 在应用程序启动时运行的代码
//Application["userCount"] = 0;
//Hashtable h = new Hashtable();
//Context.Cache.Insert("online", h);
if (Application["Visitor"] == null)
{
Application.Lock();
Application["Visitor"] = 1;
Application.UnLock();
}
else
{
Application.Lock();
Application["Visitor"] = Int32.Parse(Application["Visitor"].ToString()) + 1;
Application.UnLock();
}
}
void Application_End(object sender, EventArgs e)
{
// 在应用程序关闭时运行的代码
if(Application["Visitor"]!=null)
{
Application.Lock();
Application["Visitor"] = Int32.Parse(Application["Visitor"].ToString()) - 1;
Application.UnLock();
} }
void Application_Error(object sender, EventArgs e)
{
// 在出现未处理的错误时运行的代码 } void Session_Start(object sender, EventArgs e)
{
// 在新会话启动时运行的代码
//Application.Lock();
//Application["userCount"] = (int)Application["userCount"] + 1;
//Application.UnLock();
} void Session_End(object sender,EventArgs e)
{
// 在会话结束时运行的代码。
// 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为
// InProc 时,才会引发 Session_End 事件。如果会话模式设置为 StateServer
// 或 SQLServer,则不会引发该事件。
//Application.Lock();
//Application["userCount"] = (int)Application["userCount"] - 1;
//Application.UnLock();
//logoutCache();
}
//public void logoutCache()
//{
// Hashtable h = (Hashtable)Context.Cache["online"];
// if (h != null)
// {
// if (h[Session.SessionID] != null)
// h.Remove(Session.SessionID);
// Context.Cache["online"] = h;
// }
//}
</script>
列: id(自增标识列) Account(帐号) ActiveTime(活动时间) 备注:当用户是登录用户时, Account 字段记录其帐号,如果不是登录用户,则给 Account 字段编一个你能识别的字符串作为访客的 身份标识。(如用 id(自增标识列) 作为身份标识)
程序读取这个在线用户表来得到在线的人数: select count(distinct account) from UserOnline
2. 在每一页的 Page_Load 里加上识别用户的代码:
(1) 判断 cookie ,
如果没有 cookie ,证明这是用户打开的第一个网站里的网页,则给这个用户写一个访客的 cookie。
cookie 里保存 UserOnline 表里的三个字段。并往 UserOnline 表写一条记录。 如果已经有了 cookie ,则通过 Account(帐号) 来获取用户在 userOnline 表里的记录,并更新其 ActiveTime(活动时间)。
(2) 删除 ActiveTime(活动时间) > 5 的数据。
delete from UserOnline where datediff(minute,ActiveTime,getdate())>5 (3) 获取在线人数:
select count(distinct account) from UserOnline
您的经营之道 http://info.7ecity.com/
------------------------------------