使用asax 网站加个今日访问,总访问人数、在线人数
2010-11-29 20:45
302 查看
<%@ Application Language="C#" %>
<script runat="server">
//用于记录 总人数,今日人数,在线人数
/// <summary>
/// 总人数,今日人数,在线人数
/// </summary>
static int totalCount, todayCount, onlineCount;
/// <summary>
/// 更新今日访问访问数的标志
/// </summary>
static DateTime lastCleanUp;
/// <summary>
/// 锁对象
/// </summary>
static object _obj = new object();
/// <summary>
/// 日志文件的路径
/// </summary>
string logFile = AppDomain.CurrentDomain.BaseDirectory + "visitLog.txt";
protected void Application_Start(object sender, EventArgs e)
{
//刚启动,为了防止服务器意外死机重启等因素,需要从记录文件中读取数目
if (System.IO.File.Exists(logFile))
{
string[] lines = System.IO.File.ReadAllLines(logFile);
if (lines.Length >= 3)
{
int.TryParse(lines[0], out totalCount);
int.TryParse(lines[1], out todayCount);
DateTime.TryParse(lines[2], out lastCleanUp);
}
}
onlineCount = 0;
}
protected void Session_Start(object sender, EventArgs e)
{
//锁定对象确定单线程访问
lock (_obj)
{
//如果日期变化了,将今日访问归零
if (DateTime.Now.Day != lastCleanUp.Day)
{
lastCleanUp = DateTime.Now;
todayCount = 0;
}
//计数
todayCount++;
totalCount++;
//为了防止服务器死机重启等意外因素丢失数据,我们每隔50个访客更新一下记录文件
//这个需要根据访问量调整
if (totalCount % 50 == 0)
{
string[] fns = new string[] { totalCount.ToString(), todayCount.ToString(), lastCleanUp.ToString() };
System.IO.File.Delete(logFile);
System.IO.File.WriteAllLines(logFile, fns);
}
//在线人数加1
onlineCount++;
}
}
protected void Session_End(object sender, EventArgs e)
{
//确保不冲突
lock (_obj)
{
//在线人数减1
onlineCount--;
}
}
protected void Application_End(object sender, EventArgs e)
{
//保存当前访问
string[] fns = new string[] { totalCount.ToString(), todayCount.ToString(), lastCleanUp.ToString() };
System.IO.File.Delete(logFile);
System.IO.File.WriteAllLines(logFile, fns);
}
</script>
<script runat="server">
//用于记录 总人数,今日人数,在线人数
/// <summary>
/// 总人数,今日人数,在线人数
/// </summary>
static int totalCount, todayCount, onlineCount;
/// <summary>
/// 更新今日访问访问数的标志
/// </summary>
static DateTime lastCleanUp;
/// <summary>
/// 锁对象
/// </summary>
static object _obj = new object();
/// <summary>
/// 日志文件的路径
/// </summary>
string logFile = AppDomain.CurrentDomain.BaseDirectory + "visitLog.txt";
protected void Application_Start(object sender, EventArgs e)
{
//刚启动,为了防止服务器意外死机重启等因素,需要从记录文件中读取数目
if (System.IO.File.Exists(logFile))
{
string[] lines = System.IO.File.ReadAllLines(logFile);
if (lines.Length >= 3)
{
int.TryParse(lines[0], out totalCount);
int.TryParse(lines[1], out todayCount);
DateTime.TryParse(lines[2], out lastCleanUp);
}
}
onlineCount = 0;
}
protected void Session_Start(object sender, EventArgs e)
{
//锁定对象确定单线程访问
lock (_obj)
{
//如果日期变化了,将今日访问归零
if (DateTime.Now.Day != lastCleanUp.Day)
{
lastCleanUp = DateTime.Now;
todayCount = 0;
}
//计数
todayCount++;
totalCount++;
//为了防止服务器死机重启等意外因素丢失数据,我们每隔50个访客更新一下记录文件
//这个需要根据访问量调整
if (totalCount % 50 == 0)
{
string[] fns = new string[] { totalCount.ToString(), todayCount.ToString(), lastCleanUp.ToString() };
System.IO.File.Delete(logFile);
System.IO.File.WriteAllLines(logFile, fns);
}
//在线人数加1
onlineCount++;
}
}
protected void Session_End(object sender, EventArgs e)
{
//确保不冲突
lock (_obj)
{
//在线人数减1
onlineCount--;
}
}
protected void Application_End(object sender, EventArgs e)
{
//保存当前访问
string[] fns = new string[] { totalCount.ToString(), todayCount.ToString(), lastCleanUp.ToString() };
System.IO.File.Delete(logFile);
System.IO.File.WriteAllLines(logFile, fns);
}
</script>
相关文章推荐
- 网站今日访问,总访问及在线人数统计 asp.net
- 专题:如何实现 网站在线人数以及历史访问人数的统计
- 通过Application统计网站访问人数和在线人数
- 转:如何实现网站在线人数以及历史访问人数的统计
- Asp.Net 网站访问人数及在线人数
- 使用HttpSessionListener和ServletContextListener实现在线人数和历史访问人数统计的问题
- 在Global.asax中使用定时器来统计在线人数和每天每
- C#统计网站访问总人数和当前在线人数 Application
- C#统计网站访问总人数和当前在线人数
- 使用监听器实现一个统计网站在线人数的示例
- [ASP.NET2.0]Global.asax实现在线人数统计和站点访问总量
- 关于jsp内置对象Session和Cookie使用的肤浅理解,如何使用Session来统计网站在线人数
- 如何实现网站在线人数以及历史访问人数的统计
- 关于jsp内置对象Session和Cookie使用的肤浅理解,如何使用Session来统计网站在线人数
- 网站在线人数以及历史访问人数的统计代码
- silverlight记录网站访问总人数和在线人数的方式问题
- 使用HttpSessionListener监听器 监控当前网站在线人数
- 使用servlet统计网站在线人数的方法
- 网站在线人数以及历史访问人数的统计代码
- 使用监听器实现一个统计网站在线人数的示例