Online list of Application variables and multiple logins to the same account are prohibited

Source: Internet
Author: User

Let's talk about the implementation principle:

There are two types of List users: guests and system users.

When a user logs on to the system for the first time, he is assigned a random code (I am using his sessionid). Then, the random code is written into cookies and the expiration time is set for a long time, three years :)

Then, on the login page, first write the guest into the application ("online") variable. If the cookie value cannot be detected, the system prompts an error and cannot continue. The system prompts that the user must enable the cookies function.

When the logon is successful, first check whether the user already exists in the user list. If there are different Server Load balancer codes, you can determine that the user is logged on repeatedly and give a prompt that the user has logged on, log On again after a certain period of time. If there is no user, search for the random code and replace the corresponding array with the user data.

There are two possible reasons for user Exit: normal exit and abnormal exit.

Normal exit is well handled. You can directly Delete the array where the user is located. If the exit is abnormal, a process is called. This process is nested in all pages through a file, when any user refreshes these pages, the user list will be checked. If a user finds a timeout, the user will delete the user list data.

I have summarized the above situation into several processes, which can be easily called.

The format of application ("online") is (user name, Logon Time, random code, logon IP address |)

When no logon is performed, the user name is replaced by "guest". If logon is enabled, the user variable is replaced.

The following code is used:

This code is stored on the homepage, that is, the homepage when the user opens the system:

'Set the webpage to not cache

Response. expires =-1
Response. addheader "Pragma", "No-Cache"
Response. addheader "cache-control", "No-Cache"
'Determine whether random code cookies exist. If not, write sessionid as random code. If yes, assign it to mmcode.
If request. Cookies ("Code") = "" then
Mmcode = session. sessionid
Response. Cookies ("Code") = mmcode
Response. Cookies ("Code"). expires = dateadd ("D", 1000, date)
Else
Mmcode = request. Cookies ("Code ")
End if
Application. Lock
'Application ("online") = ""
If instr (Application ("online"), mmcode) = 0 then
Application ("online") = Application ("online") & "," & now & "," & mmcode & "," & getip & "|"
End if
Online = Split (Application ("online"), "|") 'assigns application ("online") to the array
For I = 0 to ubound (online)-1 'loops through all online users. If a random code exists, the logon time is updated to the current time,
Online1 = Split (online (I ),",")
If online1 (2) = mmcode then
Onlinetemp = online1 (0) & "," & now & "," & online1 (2) & "," & online1 (3) & "|"
Application ("online") = Replace (Application ("online"), online (I) & "|", onlinetemp)
End if
If datediff ("N", online1 (1), now)> 15 then', if the inactivity lasts for more than 15 minutes, the user's online data will be deleted.
Application ("online") = Replace (Application ("online"), online (I) & "| ","")
End if
Next
Application. Unlock

The following is a related process, which is placed on the logon page and in a proper location based on your needs:

If request. Cookies ("Code") = "" then
Response. Write "Please enable the cookie function of IE. The system requires this function to support. If you do not enable it, you will not be able to use this system! "
Response. End
Else
Mmcode = request. Cookies ("Code ")
End if

Sub onlinea () 'updates online customer information
Application. Lock
'Application ("online") = ""
If instr (Application ("online"), mmcode) = 0 then
Application ("online") = Application ("online") & "," & now & "," & mmcode & "," & getip & "|"
End if
Online = Split (Application ("online"), "|") 'assigns application ("online") to the array
For I = 0 to ubound (online)-1 'loops through all online users. If a random code exists, the logon time is updated to the current time,
Online1 = Split (online (I ),",")
If online1 (2) = mmcode then
Onlinetemp = online1 (0) & "," & now & "," & online1 (2) & "," & online1 (3) & "|"
Application ("online") = Replace (Application ("online"), online (I) & "|", onlinetemp)
End if
If datediff ("N", online1 (1), now)> 15 then', if the inactivity lasts for more than 15 minutes, the user's online data will be deleted.
Application ("online") = Replace (Application ("online"), online (I) & "| ","")
End if
Next
Application. Unlock
End sub

Sub onlineb () 'deletes online user information when you log out
Application. Lock
Online = Split (Application ("online"), "|") 'assigns application ("online") to the array
For I = 0 to ubound (online)-1 'loops through all online users. If a random code exists, the logon time is updated to the current time,
Online1 = Split (online (I ),",")
If (online1 (2) = mmcode) and (online1 (0) = SESSION ("uname") then
Application ("online") = Replace (Application ("online"), online (I) & "| ","")
Exit
End if
Next
Application. Unlock
End sub

Sub onlinec () 'update user online information upon successful logon
Application. Lock
Online = Split (Application ("online"), "|") 'assigns application ("online") to the array
For I = 0 to ubound (online)-1 'loops through all online users. If a random code exists, the logon time is updated to the current time,
Online1 = Split (online (I ),",")
If online1 (2) = mmcode then
Onlinetemp = SESSION ("uname") & "," & now & "," & online1 (2) & "," & getip & "|"
Application ("online") = Replace (Application ("online"), online (I) & "|", onlinetemp)
End if
Next
Application. Unlock
End sub

Sub checkonline () 'checks whether the current user has logged on. If the user has logged on, repeated logon is prohibited.
Online = Split (Application ("online"), "| ")
For I = 0 to ubound (online)-1
Online1 = Split (online (I ),",")
If online1 (2) <> mmcode and online1 (0) = SESSION ("uname") then
Msgstr = "this user has logged on or you are not properly logged out of the system. Please try again in 5 minutes! "
Call info1 (strtitle, msgstr, errurl)
End if
Next
End sub

The following process is put in a nested file and the last function must be called once:

Sub onlineuser () 'output user list, which can be modified as needed
Onlineusern = Split (Application ("online"), "| ")
For I = 0 to ubound (onlineusern)-1
Onlineusern1 = Split (onlineusern (I ),",")
Response. Write "& nbsp;" & onlineusern1 (0)
Next
End sub

Sub uponline () 'updates online user information
Sessioncode = request. Cookies ("Code ")
Application. Lock
Online = Split (Application ("online"), "|") 'assigns application ("online") to the array
For I = 0 to ubound (online)-1 'loops through all online users. If a random code exists, the logon time is updated to the current time,
Online1 = Split (online (I ),",")
If online1 (2) = sessioncode and Session ("uname") = online1 (0) then
Onlinetemp = online1 (0) & "," & now & "," & online1 (2) & "," & online1 (3) & "|"
Application ("online") = Replace (Application ("online"), online (I) & "|", onlinetemp)
Elseif datediff ("N", online1 (1), now)> 15 then' Delete the online data of the user if the offline activity lasts for more than 15 minutes
Application ("online") = Replace (Application ("online"), online (I) & "| ","")
End if
Next
Application. Unlock
End sub
Call uponline ()

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.