A relatively accurate "online user list" statistics function

Source: Internet
Author: User

Original: http://community.csdn.net/Expert/topic/3835/3835032.xml? Temp =. 1909601.

Thanks: afritxia (difficult to live)


When a user accesses a website, a sessionid is assigned to the user. Create a one-to-one correspondence between the IP address and the user name. If the user opens a new window, check whether the user's IP address or user name has already appeared in onlineusershash? If yes, point the new sessionid to the existing IP address. When a session ends, the sessionid is removed from onlineusers_sessioniphash. Determine whether there are other sessionids pointing to this IP address. If not, remove the user name from the online user list. The client situation is quite complex and must be fully considered. The following is the newAlgorithmOfCode:

// In the global. asax. CS File

//

// Primary key name of the online user list

Public const string key_onlineusers = "onlineusers ";

// Primary key name of the session table in the online user list

Public const string key_onlineusers_sessionip = "onlineusers_sessionip ";

Protected void application_start (Object sender, eventargs E)

{

Application. Lock ();

Application [key_onlineusers] = NULL;

Application [key_onlineusers_sessionip] = NULL; // the user's sessionid corresponds to the IP address.

Application. Unlock ();

}

 

Protected void session_start (Object sender, eventargs E)

{

Application. Lock ();

/*...*/

Hashtable onlineusershash = (hashtable) application [key_onlineusers];

Hashtable onlineuserssessioniphash = (hashtable) application [key_onlineusers_sessionip];

If (visitor. current. isguest) // if the user is a guest

{< br>
If (onlineusershash. containskey (request. userhostaddress)

{< br>
onlineusershash [request. userhostaddress] = "";

}< br>
else

{< br>
onlineusershash. add (request. userhostaddress ,"");

}< br>
else

{< br>
If (! Onlineusershash. containskey (request. userhostaddress)
&&! Onlineusershash. containsvalue (visitor. current. username)

{< br>
// if the user's IP address and user name cannot be found in the list,
onlineusershash will be added to the online user list. add (request. userhostaddress, request. cookies ["username"]. value);

}< br>
else if (onlineusershash. containsvalue (request. cookies ["username"]. value)

{< br>
// if the user's cookie information can be found, update it (delete it before adding it) IP address of an online user
// Note: The user may have logged on to Alibaba Cloud shortly after the logon because of a line failure, disconnect and dial again
// when the user returns to the website, the cookie has not expired, however, the IP address has changed.
string username = request. cookies ["username"]. value;

Foreach (Object key in onlineusershash. Keys)

{

If (string) onlineusershash [Key]). Equals (username ))

{

// Delete the IP address that the user just used

Onlineusershash. Remove (key );

Break;

}

}

// Add an online user

Onlineusershash. Add (request. userhostaddress, request. Cookies ["username"]. value );

}

Else if (onlineusershash. containskey (request. userhostaddress ))

{

// If the user's IP address can be found, the name of the online user is updated.

//

// Note: After logging on, log off and log on again. It may be to change the user name.

Onlineusershash [request. userhostaddress] = request. Cookies ["username"]. value;

}

}

// Maps the user's IP address and sessionid

If (! Onlineuserssessioniphash. containskey (session. sessionid ))

Onlineuserssessioniphash. Add (session. sessionid, request. userhostaddress );

Application. Unlock ();

}

Protected void session_end (Object sender, eventargs E)

{

Application. Lock ();

If (application [key_onlineusers]! = NULL)

{

Hashtable onlineusershash = (hashtable) application [key_onlineusers];

Hashtable onlineuserssessioniphash = (hashtable) application [key_onlineusers_sessionip];

// Obtain the user's IP address

String IP = (string) onlineuserssessioniphash [session. sessionid];

// Remove the user's IP address

Onlineuserssessioniphash. Remove (session. sessionid );

// If no session points to this IP address, it indicates that the user has indeed left the website.

// The user name can be deleted.

If (! Onlineuserssessioniphash. containsvalue (IP ))

Onlineusershash. Remove (IP );

}

Application. Unlock ();

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.