C# 统计在线人数和总访问人数
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.IO; namespace ZhengGong
{
public class Global : System.Web.HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
Application.Lock(); Application["dateTime"] = DateTime.Now.ToShortDateString(); Application["ipList"] = new List<string>(); //默认总访问记录数为0
Application["count"] = 0;
//默认当前在线数为0
Application["online"] = 0;
//将当前人数写入文件中
WriteCountPerson(0); Application.UnLock();
} protected void Session_Start(object sender, EventArgs e)
{
//临时日期和系统记录的日期对比,若不相等表示不是同一天
string tempDate = DateTime.Now.ToShortDateString();
string appDate = Application["dateTime"].ToString();
if (!tempDate.Equals(appDate))
{
Application["dateTime"] = tempDate;
Application["ipList"] = null;
int countNums = ReadCountPerson();
WriteCountPerson(countNums + int.Parse(Application["count"].ToString()));
} //发起会话的客户端IP地址
string tempIp = Context.Request.UserHostAddress;
//设置一个会话的作用时间为一分钟,即一分钟内不做任何操作的话,该会话就会失效。
Session.Timeout = 1;
//用于存储客户端的IP地址集合,若没有则表示是新的一天并且实例化出集合对象
List<string> ipList = Application["ipList"] as List<string>;
if (ipList == null)
{
ipList = new List<string>(); //如果ipList集合为空那么实例化他
} //读取出文件中保存的总访问人数
int countNums_2 = ReadCountPerson();
if (!ipList.Contains(tempIp))
{
//在ip集合中添加客户端IP地址
ipList.Add(tempIp);
Application["ipList"] = ipList;
//总访问数在文件中保存的数据累加1
countNums_2 += 1;
WriteCountPerson(countNums_2); }
//当前在线人数累加1
Application["online"] = (int)Application["online"] + 1; Application["count"] = countNums_2; Application.UnLock();
} protected void Application_BeginRequest(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(); Session.Abandon(); //当以一个会话结束后,注销该会话 int online = int.Parse(Application["online"].ToString());
if (online <= 0)
{
Application["online"] = 0;
}
else
{
Application["online"] = (int)Application["online"] - 1;
} Application.UnLock();
} protected void Application_End(object sender, EventArgs e)
{ } /// <summary>
/// 写入网页总访问人数
/// </summary>
/// <param name="nums"></param>
public void WriteCountPerson(int nums)
{
string filePath = System.Web.HttpRuntime.AppDomainAppPath + "ConfigFiles\\countPersonNums.ini";
if (!File.Exists(filePath))
{
File.Create(filePath);
}
StreamWriter sw = new StreamWriter(filePath, false);
sw.WriteLine("访问总数为:" + nums);
sw.Flush();
sw.Close();
} /// <summary>
/// 读取网页总访问人数
/// </summary>
public int ReadCountPerson()
{
try
{
int nums = 0;
string filePath = System.Web.HttpRuntime.AppDomainAppPath + "ConfigFiles\\countPersonNums.ini";
if (!File.Exists(filePath))
{
return 0;
}
FileStream fs = new FileStream(filePath, FileMode.Open);
StreamReader streamReader = new StreamReader(fs);
string strLine = streamReader.ReadLine();
string[] split = strLine.Split(':');
if (split.Length <= 1)
{
return 0;
}
int.TryParse(split[1], out nums);
fs.Flush();
fs.Close();
streamReader.Close();
streamReader.Dispose();
return nums;
}
catch (Exception ex)
{
throw ex;
}
}
}
}
直接贴出代码,使用c# 的Global.asax 全局配置文件来做处理,效果测试过大致可以但是有个小bug就是 关闭浏览器再打开浏览器那个sestion_start事件会再次执行,那么当前在线人数会有误,有待解决。
C# 统计在线人数和总访问人数的更多相关文章
- 使用Application对象简单完成网站总访问人数的统计
Global.asax文件: using System.IO; protected void Application_Start(object sender, EventArgs e) { Fil ...
- PHP统计当前网站的访问人数,访问信息,被多少次访问。
<?php header('Content-type:text/html;charset=utf-8'); //统计流量(人数,访问次数,用户IP) //假设用户访问,得到IP地址 $remo ...
- ASP.net中网站访问量统计方法代码(在线人数,本月访问,本日访问,访问流量,累计访问)
一.建立一个数据表IPStat用于存放用户信息 我在IPStat表中存放的用户信息只包括登录用户的IP(IP_Address),IP来源(IP_Src)和登录时间 (IP_DateTime),些表的信 ...
- JS在在线人数和访问人数
var date=new Date();var expiresDays=10;var count=1500+parseInt(date.getTime()/1000)-parseInt(date.ge ...
- Asp.Net 网站访问人数及在线人数
利用Application对象和Session对象可以统计历史访问人数和当前在线人数. 在会话开始和结束时,一定要进行加锁和解锁操作.由于多个用户可以共享Application对象,因此加锁是必要的, ...
- JSP使用网站访问人数统计功能,方法与技巧
实现网站访问人数统计功能的步骤: 创建静态登录页面,并指定表单提交由登录处理页面进行处理. 创建登录处理页面获得登录信息,查询数据库,判断该用户是否注册,如果该用户已注册,把已登录用户的信息保存在一个 ...
- servlet过滤器--使用过滤器统计网站访问人数的计数(注解形式)
文章目录 1.什么是过滤器? 2.过滤器核心对象 3.过滤器创建和配置 4.举例子 1.什么是过滤器? 主要用于对客户端的请求进行过滤处理,再将经过过滤后的请求转交给下一个资源. 2.过滤器核心对象 ...
- 利用php比较精确的统计在线人数的办法
利用php比较精确的统计在线人数的办法,注意这里所说的精确是指个数,如果需要精确在时间上,则需要根据实际情况调整代码中的有效时间.(自己没有写,从别人那拿过来的,先放着然后再研究)<?php// ...
- (实用篇)php精确的统计在线人数的方法
这是一个非常精确的,通过php实现统计在线人数的方法,想知道怎么实现的请耐心阅读. <?php $filename='online.txt';//数据文件 $cookiename='VGOTCN ...
随机推荐
- 详解一个自己原创的正则匹配IP的表达式
这里给大家详细讲解一下一个匹配IP地址的正则表达式, 有关正则方面的知识,会在详细的讲解中提到. 在讲解之前,我先给大家介绍一下,ip地址的生成规则. IP地址,是由32位数字二进制转为四个十进制的字 ...
- Hadoop学习笔记: 安装配置Hive
1. 在官网http://hive.apache.org/下载所需要版本的Hive,以下我们就以hive 2.1.0版为例. 2. 将下载好的压缩包放到指定文件夹解压,tar -zxvf apache ...
- (转载)两种方法让HashMap线程安全
HashMap不是线程安全的,往往在写程序时需要通过一些方法来回避.其实JDK原生的提供了2种方法让HashMap支持线程安全. 方法一:通过Collections.synchronizedMap() ...
- 基础-DP
Many years ago , in Teddy’s hometown there was a man who was called “Bone Collector”. This man like ...
- 查看SQL语句在SQL Server上的执行时间
set statistics profile onset statistics io onset statistics time ongo--begin <这里写上你的语句...> se ...
- 精益化设计:把敏捷方法和Lean UX相结合
敏捷方法已经成为了主流.同时,Kindle和iPhone等设备取得的巨大成功也推动了体验设计的飞速发展.不过,如何把敏捷方法和UX设计结合起来,一直以来都是一个难题.文章将探讨如何把UX融入到最流行的 ...
- REST WCF Service中的WebMessageBodyStyle
这个参数是个枚举包括如下值: WebMessageBodyStyle.Bare WebMessageBodyStyle.Wrapped WebMessageBodyStyle.WrappedReque ...
- netty 解决TCP粘包与拆包问题(二)
TCP以流的方式进行数据传输,上层应用协议为了对消息的区分,采用了以下几种方法. 1.消息固定长度 2.第一篇讲的回车换行符形式 3.以特殊字符作为消息结束符的形式 4.通过消息头中定义长度字段来标识 ...
- pm2.5计算和单位换算
1.pm2.5和pm10的计算 PM10a=PM10+PM25a PM25a=PM25+BC+OC+SOA1+SOA2+SOA3+SOA4+SOA5+SOA6+ANA+ASO4+ANO3+ACL+A ...
- Codeforces 747D:Winter Is Coming(贪心)
http://codeforces.com/problemset/problem/747/D 题意:有n天,k次使用冬天轮胎的机会,无限次使用夏天轮胎的机会,如果t<=0必须使用冬轮,其他随意. ...