如何清除Session/设置Session超时, 如何显示当前的访问人数和总访问量.

本文介绍如何在ASP.NET应用中实现在线人数计数功能,包括利用Application对象存储在线人数、通过Session_Start和Session_End事件调整在线人数计数、设置web.config文件中的sessionState等配置项,以及如何将在线人数数据保存到数据库。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

程序原理:
//1. 要将Application["user_sessions"]的值放到数据库中,不然,当服务器关闭时这个值就会被删除。
//2. 在Global.asax文件中 根根Session 来设置Application["user_sessions"]值的加减.
//3. 也要注意在web.config文件中设置Session 在下面讲了。
//4. 建立一个数据库表。
//5. 在数据库表中设初始值(可以设大一点)。以便访问量让别人看起来访问量会很大。
//6. 将获得的新的Application["user_sessions"]的值 Update到数据库表中。


//========显示访问的人数
 <script language="c#" runat="server">
        private void Page_Load(object sender, System.EventArgs e)
        {
            this.visitors.Text = "本站当前有: <br>" + Application["user_sessions"].ToString() + "" + "<br> 位访问者 !";
        }
</script>
//====Global.asax文件

 1None.gifusing System;
 2None.gifusing System.Collections;
 3None.gifusing System.ComponentModel;
 4None.gifusing System.Web;
 5None.gifusing System.Web.SessionState;
 6None.gif
 7None.gifnamespace BYD_SPCSystem 
 8ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 9ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
10InBlock.gif    /// Global 的摘要说明。
11ExpandedSubBlockEnd.gif    /// </summary>

12InBlock.gif    public class Global : System.Web.HttpApplication
13ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
14ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
15InBlock.gif        /// 必需的设计器变量。
16ExpandedSubBlockEnd.gif        /// </summary>

17InBlock.gif        private System.ComponentModel.IContainer components = null;
18InBlock.gif
19InBlock.gif        public Global()
20ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
21InBlock.gif            InitializeComponent();
22ExpandedSubBlockEnd.gif        }
    
23InBlock.gif        
24InBlock.gif        protected void Application_Start(Object sender, EventArgs e)
25ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
26InBlock.gif            //在HttpApplication 类的第一个实例被创建时,该事件被触发。它允许你创建可以由所有HttpApplication 实例访问的对象。
27InBlock.gif            Application["user_sessions"= 100;
28ExpandedSubBlockEnd.gif        }

29InBlock.gif 
30InBlock.gif        protected void Session_Start(Object sender, EventArgs e)
31ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
32InBlock.gif            //session的生存期是多长,是可以设定的,Session.timeout=X(分钟)
33InBlock.gif            //在一个新用户访问应用程序 Web 站点时,该事件被触发。
34InBlock.gif            Application.Lock();
35InBlock.gif            Application["user_sessions"= (int)Application["user_sessions"+ 1;
36InBlock.gif            Application.UnLock();
37InBlock.gif
38ExpandedSubBlockEnd.gif        }

39InBlock.gif
40InBlock.gif        protected void Application_BeginRequest(Object sender, EventArgs e)
41ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
42InBlock.gif
43ExpandedSubBlockEnd.gif        }

44InBlock.gif
45InBlock.gif        protected void Application_EndRequest(Object sender, EventArgs e)
46ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
47InBlock.gif
48ExpandedSubBlockEnd.gif        }

49InBlock.gif
50InBlock.gif        protected void Application_AuthenticateRequest(Object sender, EventArgs e)
51ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
52InBlock.gif
53ExpandedSubBlockEnd.gif        }

54InBlock.gif
55InBlock.gif        protected void Application_Error(Object sender, EventArgs e)
56ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
57InBlock.gif
58ExpandedSubBlockEnd.gif        }

59InBlock.gif
60InBlock.gif        protected void Session_End(Object sender, EventArgs e)
61ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
62InBlock.gif            //在一个用户的会话超时、结束或他们离开应用程序 Web 站点时,该事件被触发。
63InBlock.gif            Application.Lock();
64InBlock.gif            Application["user_sessions"= (int)Application["user_sessions"]-1;
65InBlock.gif            Application.UnLock();
66ExpandedSubBlockEnd.gif        }

67InBlock.gif
68InBlock.gif        protected void Application_End(Object sender, EventArgs e)
69ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
70InBlock.gif
71ExpandedSubBlockEnd.gif        }

72InBlock.gif            
73ContractedSubBlock.gifExpandedSubBlockStart.gif        Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
74ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
75InBlock.gif        /// 设计器支持所需的方法 - 不要使用代码编辑器修改
76InBlock.gif        /// 此方法的内容。
77ExpandedSubBlockEnd.gif        /// </summary>

78InBlock.gif        private void InitializeComponent()
79ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{    
80InBlock.gif            this.components = new System.ComponentModel.Container();
81ExpandedSubBlockEnd.gif        }

82ExpandedSubBlockEnd.gif        #endregion

83ExpandedSubBlockEnd.gif    }

84ExpandedBlockEnd.gif}

85None.gif
86None.gif


//=====web.config文件

ContractedBlock.gifExpandedBlockStart.gif
1None.gif<sessionState mode="InProc" stateConnectionString="tcpip=127.0.0.1:42424" 
2None.gif                      sqlConnectionString="data source=127.0.0.1;Trusted_Connection=yes" 
3None.gif                      cookieless="true" timeout="20"/>



//删除Session   
//================1. 关闭网页时加载另一个页面
<script type="text/javascript">
var obj = new Object();
obj.name="i5tt";
function window.onunload()
{
window.showModalDialog("ClosePage.aspx",obj,"dialogWidth=200px;dialogHeight=100px");
}
</script>

//========2. 清空Session  
 <%Session.Clear(); Session.Abandon();  %>
//========3. 将这个页面关闭.
protected void Page_Load(object sender, EventArgs e)
        {
            Response.Write("<script language='javascript'>window.close();</script>");
        }
//========4. 注意这样清除Session的话,在每次刷新页面时都会将Session自动清空,因为Session是在页面加载时清空的。 
  -->1. 刷新是用来更新当前数据的显示信息
         -->2. 重新提交页面. 

转载于:https://www.cnblogs.com/wj-wangjun/archive/2007/08/11/851647.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值