Implementation ideas:
After a user logs on successfully, the user logon information is stored in hashtable application ["online"]. The key value is sessionid and the value is user ID. When the user logs off, call session. abandon; in global. in the sessionend event in asax, the user ID is deleted from hashtable. When the user accesses the page, the user cannot have the corresponding user ID in hashtable. If not, the user is judged to be offline.
1. functions used to determine whether a user can be online (called by the user)
Code
/** // <Summary>
/// Determine whether the struserid can be included in hashtable H.
/// </Summary>
/// <Param name = "struserid"> </param>
/// <Param name = "H"> </param>
/// <Returns> </returns>
Public static bool amionline (string strusername, hashtable H)
{
If (strusername = NULL)
Return false;
// Continue to determine if the user has logged on
If (H = NULL)
Return false;
// Determine whether the user exists in the hash table.
Idictionaryenumerator e1 = H. getenumerator ();
Bool flag = false;
While (e1.movenext ())
{
If (e1.value. tostring (). compareto (strusername) = 0)
{
Flag = true;
Break;
}
}
Return flag;
}
2. handling user logon events:
Code
Private void btnlogin_click (Object sender, system. Web. UI. imageclickeventargs E)
{
// User is a custom class, which contains the login Essentials
User curuser = new user ();
Curuser. Username = This. username. Text. Trim ();
If (myutility. amionline (curuser. username, (hashtable) application ["online"])
{
Response. Write ("<SCRIPT> alert ('the logon ID you are using is already online! You cannot log on again! '); </SCRIPT> ");
Return;
}
Else
{
Hashtable H = (hashtable) application ["online"];
If (H = NULL)
H = new hashtable ();
H [session. sessionid] = curuser. Username;
Application ["online"] = h;
}
}
3. session_end event in global. asax:
Code
Protected void session_end (Object sender, eventargs E)
{
Hashtable H = (hashtable) application ["online"];
If (H [session. sessionid]! = NULL)
H. Remove (session. sessionid );
Application ["online"] = h;
}
According to the online data, the key is to refresh the idea!
<Move against the water, move back if you do not enter>