When performing online user statistics and tracking, it is found that there is always a statistical error,
Check the old version. The number of online users is always greater than the actual number of online users. The test is performed several times. The reason is that the exited session does not expire immediately. The timeout setting of the session is too long. After the page is closed, the sessionid is not deleted. I tried it many times and finally found a method that immediately invalidates the session after the browser is closed.
This version simulates the first trace after a user logs on. Download here:
I made the following changes:
1. When a logon jump is made, the existing user name will be passed into the cache and the user name will be updated.
Userinfo uinfo = new userinfo ();
Uinfo. sessionid = This. session. sessionid;
If (httpcontext. Current. cache [uinfo. cachekey]! = NULL)
{
(Userinfo) httpcontext. Current. cache [uinfo. cachekey]). Username = textbox1.text. Trim ();
}
2. When you close the page after logging on, call the client onUnload = "removeline ()" and execute the following function:
<Script language = "JavaScript">
Function removeline () {If (event. clientx <0 & event. clienty <0)
{
Document. write ('<IFRAME width = "100" Height = "100" src = "removesession. aspx "> </iframe> <object classid = CLSID: 8856f961-340a-11d0-a96b-00c04fd705a2 Height = 0 id = webbrowser width = 0> </Object> '); document. all. webbrowser. execwb (45, 1 );
}
}
</SCRIPT>
The onUnload Event determines that the page_load event in removesession. aspx is executed when the event is closed.
In removesession. aspx, page_load and execute the following statement:
Session. removeall ();
Session. Abandon ();
If (httpcontext. Current. cache [This. session. sessionid]! = NULL)
{Httpcontext. Current. cache. Remove (this. session. sessionid );
}
After this modification, the sessionid in the cache is manually deleted, so that the online statistics are finally accurate, so I'm a little angry :)