显示在线人数

通过显示在线人数从而得知当前用户的数量

.aspx文件代码:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace test
{
    public partial class WebForm1 : System.Web.UI.Page
    {
         //下载页面时,将Application["user_sessions"]状态的值取出
        protected void Page_Load(object sender, EventArgs e)
        {
          
            string strCounter = Application["user_sessions"].ToString();
            string imgName;

            Response.Write("本站当前有");
            for (int i = 0; i < strCounter.Length; i++)
            {
                imgName = strCounter.Substring(i, 1);
                Response.Write(imgName);
            }
            Response.Write("位访问者");
        }
    }
}

global.asax文件:

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.Xml.Linq;

namespace test
{
    public class application : System.Web.HttpApplication
    {
         //第一位访客链接到应用程序时,触发Application-Start事件,以一个在整个
         //"应用程序范围"的Application对象Application["user_sesions"]将在线人数置0
        protected void Application_Start(object sender, EventArgs e)
        {
            Application["user_sessions"] = 0;
        }
          //每一位访客链接页面时,触发Session-Start事件创建自己的会话状态。
          //为Application["user_sessions"]状态加1
        protected void Session_Start(object sender, EventArgs e)
        {      //锁定应用程序状态
            Application.Lock();
               //为应用程序状态加1
            Application["user_sessions"]=(int)Application["user_sessions"]+1;
              //解除锁定
            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)
        {

        }
          //访客断开链接或访问超时时,觖发Session-End事件,为访客断开会话状态。
          //在此过程中,Application["user_sessions"]状态减1
        protected void Session_End(object sender, EventArgs e)
        {
            Application.Lock();
            Application["user_sessions"] = (int)Application["user_sessions"] - 1;
            Application.UnLock();
        }

        protected void Application_End(object sender, EventArgs e)
        {

        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值