日期:2014-05-18  浏览次数:20410 次

如何统计网站的浏览量?
如题:
 急!!!
 请帮帮忙 !!
 要在首页显示,不需要那种免费的统计流量的系统。。只需要再首页显示即可!!

------解决方案--------------------
用session嘛,累加,写进数据库,在页面上加载。
------解决方案--------------------

在网站中添加一个Global.asax文件


<%@ Application Language="C#" %>
<%@ Import Namespace="System.IO"%>
<script runat="server">

void Application_Start(object sender, EventArgs e) 
{
// 在应用程序启动时运行的代码
StreamReader reader = new StreamReader(Server.MapPath("count.txt"));
Application.Lock();
Application["count"] = reader.ReadLine();//网站访问量
reader.Close();
Application["online"] = 0; //同时在线人数
Application.UnLock();

}

void Application_End(object sender, EventArgs e) 
{
// 在应用程序关闭时运行的代码

}

void Application_Error(object sender, EventArgs e) 

// 在出现未处理的错误时运行的代码

}

void Session_Start(object sender, EventArgs e) 
{
// 在新会话启动时运行的代码

Application.Lock();
Application["count"] = Convert.ToInt32(Application["count"])+1; 
Application["online"] = Convert.ToInt32(Application["online"])+1;
Application.UnLock();
StreamWriter writer = new StreamWriter(Server.MapPath("count.txt"));
writer.Write(Convert.ToInt32(Application["count"]));
writer.Close();

}

void Session_End(object sender, EventArgs e) 
{
// 在会话结束时运行的代码。 
// 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为
// InProc 时,才会引发 Session_End 事件。如果会话模式设置为 StateServer 
// 或 SQLServer,则不会引发该事件。
Application["online"] = Convert.ToInt32(Application["online"]) - 1;
}

</script>

2. 创建一个文本文件count.txt , 在文件中先添加一个数字"0"。

3. 在需要显示网站访问量与在线人数的网页上添加两个Label, 一个按钮,相关事件代码如下:

public partial class _Default : System.Web.UI.Page 
{
protected void Page_Load(object sender, EventArgs e)
{
this.Label1.Text = Application["count"].ToString();
this.Label2.Text = Application["online"].ToString();

}
protected void Button1_Click(object sender, EventArgs e)
{
Session.Abandon();
Response.Write("<script>window.opener=null;window.close();</script>");
}
}

------解决方案--------------------
最简单的,一个数据库表,在首页查询下再累加一下更新表。
------解决方案--------------------
存在问题多多啊,感觉还是放库里安全点。notepad丢失了,该功能等于没有了。
------解决方案--------------------
最好放在数据库中比较好。安全起见。这样管理不好容易出乱子。
------解决方案--------------------
建表:
SQL code
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[frmVisitCount]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[frmVisitCount]
GO

CREATE TABLE [dbo].[frmVisitCount] (
    [ID] [int] IDENTITY (1, 1) NOT NULL ,
    [ToDayCount] [bigint] NULL ,
    [AllCount] [bigint] NULL ,
    [LastVisitDate] [datetime] NULL 
) ON [PRIMARY]
GO

------解决方案--------------------
up
hanbb1982已经写的很详细了