开发者

User count with sessionstate set as sqlserver

开发者 https://www.devze.com 2023-01-22 08:21 出处:网络
I want to know no. of active users on my website with sessionstate set as sqlserver. And what if a per开发者_运维百科son closes his browser then how to maintain the user count?

I want to know no. of active users on my website with sessionstate set as sqlserver.

And what if a per开发者_运维百科son closes his browser then how to maintain the user count?

Any help appreciated.

Thanks in advance


If you use SQL Server as session state, than the dateabase ASPState is used, and it has following tables.

  1. ASPStateTempSessions
  2. ASPStateTempApplications

and several stored procedures and jobs. you can explore it to understand.

so if you have only single application, than it is just a simple query.

select count(*) as NumberOfSessions from ASPStateTempSessions

In case if it is configured with multiple application using Application Name in connection string, than you need sessionid in table ASPStateTempSessions, so

select count(*) as NumberOfSessions from ASPStateTempSessions where sessionid='%YOUR-APP-ID'


What web server code are you using? If you are using ASP.NET, you can increment a variable in Session_Start in your Global.asax file, then write that variable to SQL Server when your web application shuts down in Application_End. Make sure you lock the variable as two threads may be accessing the variable at the same time if two simultaneous connections come in. Not sure if this answers your question. I see you have the word "active" users. If that means ones that are currently on, you can decrement the counter in Session_End. Then, sometime after the user closes the browser, that user's session will time out. You can't decrement right when they close the browser as your web server will have know way of knowing that.

0

精彩评论

暂无评论...
验证码 换一张
取 消