在线用户的统计方法有很多其中一个就是SESSION保存在数据库,然后select tmpdb表,获取在线用户. 下面介绍一个非常有用的办法试用Cache, Cache是服务器的方法,跟ASP的Application差不多, 下面是一个Cache在线用户的超作, 流程:建立一个DataTable 把需要的字段添加进去, id,userid,username,place,lasttime lasttime是最后活动的时间,这个时间是决定用户是否超时登陆,然后用一个方法来删除过时的用户
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Caching;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
-
/// <summary>
-
-
/// OnlineCache 的摘要说明
-
-
/// </summary>
-
-
public class OnlineCache
-
-
{
-
-
public OnlineCache()
-
-
{
-
-
-
-
}
-
-
-
-
#region 缓存在线用户
-
-
/// <summary>
-
-
/// 建立缓存表
-
-
/// </summary>
-
-
public static void BuildCacheOnlineTable()
-
-
{
-
-
DataTable dt = new DataTable();
-
-
DataColumn col1 = dt.Columns.Add("ID", typeof(Int32));
-
-
col1.AllowDBNull = false;
-
-
col1.AutoIncrement = true;
-
-
col1.AutoIncrementSeed = 1;
-
-
col1.AutoIncrementStep = 1;
-
-
col1.Unique = true;
-
-
-
-
DataColumn col2 = dt.Columns.Add("UserID", typeof(String));
-
-
col2.AllowDBNull = true;
-
-
-
-
DataColumn col3 = dt.Columns.Add("UserName", typeof(String));
-
-
col3.AllowDBNull = true;
-
-
-
-
DataColumn col4 = dt.Columns.Add("UserPlace", typeof(String));
-
-
col4.AllowDBNull = true;
-
-
-
-
DataColumn col5 = dt.Columns.Add("IP", typeof(String));
-
-
col5.AllowDBNull = true;
-
-
-
-
DataColumn col6 = dt.Columns.Add("LastActiveTime", typeof(DateTime));
-
-
col6.AllowDBNull = true;
-
-
col6.DefaultValue = DateTime.Now;
-
-
-
-
-
-
HttpContext.Current.Cache.Add("UserOnlineTable", dt, null, DateTime.Now.AddYears(10), TimeSpan.Zero, CacheItemPriority.Normal, null);
-
-
}
-
-
-
-
-
-
/// <summary>
-
-
/// 获取当前位置
-
-
/// </summary>
-
-
/// <returns></returns>
-
-
public static string GetPlace()
-
-
{
-
-
string str = "首页";
-
-
if (HttpContext.Current.Request.Url != null)
-
-
{
-
-
string url = HttpContext.Current.Request.Url.AbsolutePath.ToLower();
-
-
if (url.IndexOf("default.aspx") >= 0)
-
-
{
-
-
str = "首页";
-
-
}
-
-
else if (url.IndexOf("detail.aspx") >= 0)
-
-
{
-
-
str = "查看详细";
-
-
}
-
-
else if (url.IndexOf("getpassword.aspx") >= 0)
-
-
{
-
-
str = "取回密码";
-
-
}
-
-
else if (url.IndexOf("gettry.aspx") >= 0)
-
-
{
-
-
str = "获取试用";
-
-
}
-
-
else if (url.IndexOf("gopay.aspx") >= 0)
-
-
{
-
-
str = "付款";
-
-
}
-
-
else if (url.IndexOf("modpass.aspx") >= 0)
-
-
{
-
-
str = "修改密码";
-
-
}
-
-
else if (url.IndexOf("modprofile.aspx") >= 0)
-
-
{
-
-
str = "修改资料";
-
-
}
-
-
else if (url.IndexOf("myorder.aspx") >= 0)
-
-
{
-
-
str = "我的订单";
-
-
}
-
-
else if (url.IndexOf("myreport.aspx") >= 0)
-
-
{
-
-
str = "我的报告";
-
-
}
-
-
else if (url.IndexOf("register.aspx") >= 0)
-
-
{
-
-
str = "注册";
-
-
}
-
-
else if (url.IndexOf("writereport.aspx") >= 0)
-
-
{
-
-
str = "写报告";
-
-
}
-
-
else if (url.IndexOf("forumlist.aspx") >= 0)
-
-
{
-
-
str = "论坛列表";
-
-
}
-
-
else if (url.IndexOf("forummail.aspx") >= 0)
-
-
{
-
-
str = "论坛邮箱";
-
-
}
-
-
else if (url.IndexOf("forumsetsign.aspx") >= 0)
-
-
{
-
-
str = "修改个人签名";
-
-
}
-
-
else if (url.IndexOf("forumtopic.aspx") >= 0)
-
-
{
-
-
str = "浏览贴子";
-
-
}
-
-
else if (url.IndexOf("forum.aspx") >= 0)
-
-
{
-
-
str = "论坛首页";
-
-
}
-
-
else if (url.IndexOf("list.aspx") >= 0)
-
-
{
-
-
str = "查看类别";
-
-
}
-
-
}
-
-
return str;
-
-
}
-
-
-
-
-
-
/// <summary>
-
-
/// 统计在线人数
-
-
/// </summary>
-
-
/// <returns></returns>
-
-
public static int OnlineCount()
-
-
{
-
-
if (HttpContext.Current.Cache["UserOnlineTable"] == null)
-
-
BuildCacheOnlineTable();
-
-
-
-
DataTable dt = (DataTable)HttpContext.Current.Cache["UserOnlineTable"];
-
-
-
-
return dt.Rows.Count;
-
-
}
-
-
-
-
/// <summary>
-
-
/// 获取在线用户列表
-
-
/// </summary>
-
-
/// <returns></returns>
-
-
public static DataTable GetOnlineUserTable()
-
-
{
-
-
ClearExptionUser();
-
-
return (DataTable)HttpContext.Current.Cache["UserOnlineTable"];
-
-
-
-
}
-
-
-
-
/// <summary>
-
-
/// 检查IP是否在Cache中
-
-
/// </summary>
-
-
/// <param name="ip"></param>
-
-
/// <returns></returns>
-
-
public static bool CheckOnlineIP(string ip)
-
-
{
-
-
if (HttpContext.Current.Cache["UserOnlineTable"] == null)
-
-
BuildCacheOnlineTable();
-
-
DataTable dt = (DataTable)HttpContext.Current.Cache["UserOnlineTable"];
-
-
DataRow[] drs = dt.Select("ip='" + ip + "'");
-
-
bool result = false;
-
-
if (drs.Length > 0)
-
-
result = true;
-
-
-
-
return result;
-
-
}
-
-
-
-
/// <summary>
-
-
/// 用户是否在线
-
-
/// </summary>
-
-
/// <param name="userid"></param>
-
-
/// <returns></returns>
-
-
public static bool IsOnline(string userid)
-
-
{
-
-
if (HttpContext.Current.Cache["UserOnlineTable"] == null)
-
-
BuildCacheOnlineTable();
-
-
-
-
DataTable dt = (DataTable)HttpContext.Current.Cache["UserOnlineTable"];
-
-
DataRow[] drs = dt.Select("userid='" + userid + "'");
-
-
bool result = false;
-
-
if (drs.Length > 0)
-
-
result = true;
-
-
-
-
return result;
-
-
}
-
-
-
-
/// <summary>
-
-
/// 删除过时用户
-
-
/// </summary>
-
-
public static void ClearExptionUser()
-
-
{
-
-
if (HttpContext.Current.Cache["UserOnlineTable"] == null)
-
-
BuildCacheOnlineTable();
-
-
-
-
DataTable dt = (DataTable)HttpContext.Current.Cache["UserOnlineTable"];
-
-
for (int i = 0; i < dt.Rows.Count; i++)
-
-
{
-
-
DateTime lastactive = Convert.ToDateTime(dt.Rows[i]["LastActiveTime"]);
-
-
int diff = Convert.ToInt32(Unit.DateDiff("minute", lastactive, DateTime.Now));
-
-
if (diff > 20)
-
-
{
-
-
dt.Rows[i].Delete();
-
-
}
-
-
}
-
-
HttpContext.Current.Cache.Add("UserOnlineTable", dt, null, DateTime.Now.AddYears(10), TimeSpan.Zero, CacheItemPriority.Normal, null);
-
-
}
-
-
-
-
/// <summary>
-
-
/// 更新用户活动时间
-
-
/// </summary>
-
-
/// <param name="userid"></param>
-
-
public static void UpdateOnline(string userid, string place)
-
-
{
-
-
if (HttpContext.Current.Cache["UserOnlineTable"] == null)
-
-
BuildCacheOnlineTable();
-
-
-
-
DataTable dt = (DataTable)HttpContext.Current.Cache["UserOnlineTable"];
-
-
DataRow[] drs = dt.Select("userid='" + userid + "'");
-
-
for (int i = 0; i < drs.Length; i++)
-
-
{
-
-
drs[i]["LastActiveTime"] = DateTime.Now;
-
-
drs[i]["UserPlace"] = place;
-
-
}
-
-
HttpContext.Current.Cache.Add("UserOnlineTable", dt, null, DateTime.Now.AddYears(10), TimeSpan.Zero, CacheItemPriority.Normal, null);
-
-
}
-
-
-
-
public static void UpdateOnlineByIP(string userid, string username, string userplace, string ip)
-
-
{
-
-
if (HttpContext.Current.Cache["UserOnlineTable"] == null)
-
-
BuildCacheOnlineTable();
-
-
-
-
DataTable dt = (DataTable)HttpContext.Current.Cache["UserOnlineTable"];
-
-
DataRow[] drs = dt.Select("ip='" + ip + "'");
-
-
for (int i = 0; i < drs.Length; i++)
-
-
{
-
-
drs[i]["LastActiveTime"] = DateTime.Now;
-
-
drs[i]["UserID"] = userid;
-
-
drs[i]["UserName"] = username;
-
-
drs[i]["UserPlace"] = userplace;
-
-
}
-
-
HttpContext.Current.Cache.Add("UserOnlineTable", dt, null, DateTime.Now.AddYears(10), TimeSpan.Zero, CacheItemPriority.Normal, null);
-
-
}
-
-
-
-
public static void DeleteOnlineUser(string userid)
-
-
{
-
-
if (HttpContext.Current.Cache["UserOnlineTable"] == null)
-
-
BuildCacheOnlineTable();
-
-
DataTable dt = (DataTable)HttpContext.Current.Cache["UserOnlineTable"];
-
-
DataRow[] drs = dt.Select("userid='" + userid + "'");
-
-
for (int i = 0; i < drs.Length; i++)
-
-
{
-
-
dt.Rows.Remove(drs[i]);
-
-
}
-
-
HttpContext.Current.Cache.Add("UserOnlineTable", dt, null, DateTime.Now.AddYears(10), TimeSpan.Zero, CacheItemPriority.Normal, null);
-
-
}
-
-
-
-
-
-
public static void InsertOnlineUser(string userid, string username, string UserPlace, string IP)
-
-
{
-
-
if (HttpContext.Current.Cache["UserOnlineTable"] == null)
-
-
BuildCacheOnlineTable();
-
-
-
-
DataTable dt = (DataTable)HttpContext.Current.Cache["UserOnlineTable"];
-
-
DataRow workRow;
-
-
workRow = dt.NewRow();
-
-
workRow["UserID"] = userid;
-
-
workRow["UserName"] = username;
-
-
workRow["UserPlace"] = UserPlace;
-
-
workRow["IP"] = IP;
-
-
workRow["LastActiveTime"] = DateTime.Now;
-
-
dt.Rows.Add(workRow);
-
-
HttpContext.Current.Cache.Add("UserOnlineTable", dt, null, DateTime.Now.AddYears(10), TimeSpan.Zero, CacheItemPriority.Normal, null);
-
-
ClearExptionUser();
-
}
-
-
-
-
#endregion
-
-
}