日访问量统计 Global.asax运行不起来
<%@ Application Language="C#" %>
<%@ Import Namespace = "System.IO" %>
<script runat="server">
void Application_Start(object sender, EventArgs e)
{
//在应用程序启动时运行的代码
Application["counter"] = 0;
Application["day"] = DateTime.Now.ToString();
}
void Application_End(object sender, EventArgs e)
{
//在应用程序关闭时运行的代码
int Stat = 0;
Stat = (int)Application["counter"];
string day0 = (string)Application["day"];
string str = Stat.ToString() + "," + day0.ToString;
string file_path = Server.MapPath("counter.txt");
StreamWriter srw = new StreamWriter(file_path, false);
srw.WriteLine(str);
srw.Close();
}
void Application_Error(object sender, EventArgs e)
{
//在出现未处理的错误时运行的代码.
}
void Session_Start(object sender, EventArgs e)
{
//在新会话启动时运行的代码
Application.Lock();
int count;
string NowDay;
string strs = string.Empty;
string day;
StreamReader srd;
string file_pth = Server.MapPath("counter.txt");
srd = File.OpenText(file_pth);
while (srd.Peek() != -1)
{
strs = srd.ReadLine();
}
srd.Close;
string[] str = strs.Split(',');
count = Convert.ToInt32(str[0]);
day = str[1];
NowDay = DateTime.Now.ToString();
if (DateTime.Compare(Convert.ToDateTime(NowDay),Convert.ToDateTime(day)) >=0)
{
count = 0;
day = DateTime.Now.AddDays(1).ToShortDateString() + " " + "00;00;00";
string NewDayStr = "0" + "," + day.ToString();
StreamWriter srw0 = new StreamWriter(file_pth,false);
srw0.WriteLine(NewDayStr);
srw0.Close();
}
object objcount = count;
object objday = DayNameFormat;
Application["counter"] = objcount;
Application["day"] = objday;
int Stat = 0;
Stat = (int)Application["counter"];
Stat += 1;
object obj = Stat;
Application["counter"] = obj;
string day0 = (string)Application["day"];
string str0 = obj.ToString() + "," + day0.ToString();
string file_path0 = Server.MapPath("counter.txt");
StreamWriter srw1 = new StreamWriter(file_pth,false);
srw1.WriteLine(str0);
srw1.Close();
Application.UnLock();
}
void Session_End(object sender, EventArgs e)
{
//在会话结束时运行的代码。
// 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为
// InProc 时,才会引发 Session_End 事件。如果会话模式
//设置为 StateServer 或 SQLServer,则不会引发该事件
}
</script>
public partial class _Default : System.Web.UI.Page--------------------编程问答-------------------- 代码太复杂了,一上来从一个最简单的开始试。别一下子就把什么读写文件之类的东西全带进来。 --------------------编程问答--------------------
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
if (Application["counter"] == null)
{
TextBox1.Text = "1";
}
else
{
object counter = Application["counter"];
TextBox1.Text = counter.ToString();
}
}
}
意思我是知道的 只是写法可能出来问题 这个允许时候Global.asax报错 --------------------编程问答-------------------- Refer:
http://www.cnblogs.com/insus/archive/2013/05/30/3107681.html
http://www.cnblogs.com/insus/archive/2009/03/28/1423853.html
see also:
http://www.cnblogs.com/insus/p/3291322.html --------------------编程问答--------------------
当前在线人数楼主是用session统计的吗?可以在Application_Start里面使session +1--------------------编程问答-------------------- 如果可以的话,建议使用第三方的统计,如51la,cnzz等,通过第三方来统计或许是一个不错的解决方案 --------------------编程问答-------------------- 我上面写的方法是每天记录的(存放txt文档里面) 每激发会话就+1 结束会话不用减 只求每天访问量
在 Application_End里session -1;
00:00:00分时重新计算
补充:.NET技术 , ASP.NET