PDA

View Full Version : Preventing the same username from logging in twice


rwmoulton
06-23-2003, 12:07 PM
Client wants to prevent anyone from using a username that is currently logged into the web application. I currently store each User's information in Session Variables, but I don't think I can check all values for 'Session("UserID")' to see if it's currently being used.

Is there a way to do this with Application variables?

Frank
06-23-2003, 09:45 PM
Is there a way? Yes, there is.

Is it easy? Well, no, not really...

You could use an array to store those names in, and it is possible to store that entire array in an application variable.

I wrote a chat application that used a similar method for the user list. I did not prevent multiple names from logging in, but rather uptaded the user's "timeout" if they were already logged in...

Of course that is irrelavent. I will try to post some of the code so that you can look at it and possibly adapt it for what you are trying to do, but I can't garantee it will work without causing you to forceably remove much of the hair on your head ;)

Keep in mind, that I wrote this for an entirely different type of application, and a very complex application at that. Each function I needed was originally written in the form of subs that I could call from different frames. As a result you will probably have to do some extensive modification. Hopefully it will help give you an idea of what you can do. I am assuming that you have a good enough knowledge of ASP to be able to determine what most of this code does. Good luck!

I will post each sub (AddName, RemoveName, UpdateList) in a different post so that I don't have one huge post here.

This first section will check the list, to make sure it is an array, and will add the username to it if it is not already on the list.

The array (arrUsers) is a 2-dimensional array. The first element contains the user's name, and the second element contains the "timeout", which is what I used as a way to work around having a user's name on the list forever even after they leave the chat room. I did this simply due to the fact that all users will not click the EXIT link. (I hope you understand arrays pretty good)

So the array would look like this:
arrUsers(0,0) is the username
arrUsers(0,1) is the timeout
(which is Now() + 1/1440. This adds one minute to the current time)

This code pulls the array from an application variable, then looks at the array to see if the username is there or not. If it is then it will update the "timeout" but in your case, you would generate your error message. If the name isn't found, I create a temporary array that has 1 more element than the original list array, move all of the names in the original to the new array, add the new name to the last element of the new array, and then store the new array in the application variable.

This probably is not the easiest or most efficient way of doing this but it worked for my purposes...and seemed to be reliable.
------------------------------------


Sub AddName(strName)

If strName <> "" Then
If IsArray(Application("Users")) Then
bolUserFound = False
arrUsers = Application("Users")
intUserCount = Ubound(arrUsers)
intNewUser = intUserCount + 1

'This section looks for the username in the list. If it finds it
'the "timeout" for the name is updated. If the name is
'already on the list, the variable bolUserFound will be set to
'true
For intCounter = 0 to intUserCount
If Lcase(arrUsers(intCounter, 0)) = Lcase(strName) Then
'You can probably remove the next line since you
'don't want multiple logins
arrUsers(intCounter, 1) = Now() + 1/1440
'keep the next line
bolUserFound = True
End If
Next

If Not bolUserFound Then
ReDim arrTempArray(intNewUser, 1)
For intCounter = 0 to intUserCount
arrTempArray(intCounter, 0) = arrUsers(intCounter, 0)
arrTempArray(intCounter, 1) = arrUsers(intCounter, 1)
Next

arrTempArray(intNewUser, 0) = strName
arrTempArray(intNewUser, 1) = Now() + 1/1440

APPLICATION.LOCK
Application("Users") = arrTempArray
APPLICATION.UNLOCK
Else
'If the name is already on the list you would probably
'want to put your error message or redirect here
APPLICATION.LOCK
Application("Users") = arrUsers
APPLICATION.UNLOCK
End If
Else
ReDim arrTempArray(0, 1)
arrTempArray(0, 0) = strName
arrTempArray(0, 1) = Time

APPLICATION.LOCK
Application("Users") = arrTempArray
APPLICATION.UNLOCK
End If
End If

End Sub


---------------
Next section is coming as soon as I get a few minor mods done to it so that it doesn't confuse you ;} These were all written to handle multiple user lists, and not just one. Sorry about the complexity of this but like I said, it was written for a complex application.

Frank
06-23-2003, 09:53 PM
This section will remove the name from the list. Again this will probably need extensive modification to work with your app.
-------------------------------------


Sub RemoveName(strName)

arrUsers = Application("Users")
intUserCount = Ubound(arrUsers)
intNewCount = intUserCount

For intCounter = 0 to intUserCount
If Lcase(arrUsers(intCounter, 0)) = Lcase(strName) Then
arrUsers(intCounter, 0) = ""
arrUsers(intCounter, 1) = ""
End If
Next

For intCounter = 0 to intUserCount
If arrUsers(intCounter, 0) = "" Then
intNewCount = intNewCount - 1
End If
Next

ReDim arrTempArray(intNewCount, 1)
intTempCount = 0

For intCounter = 0 to intUserCount
If arrUsers(intCounter, 0) <> "" Then
arrTempArray(intTempCount, 0) = arrUsers(intCounter, 0)
arrTempArray(intTempCount, 1) = arrUsers(intCounter, 1)
intTempCount = intTempCount + 1
End If
Next

APPLICATION.LOCK
Application("Users") = arrTempArray
APPLICATION.UNLOCK

End Sub

Frank
06-23-2003, 10:04 PM
Now this section updates the "timeout" for each user. It is designed to remove the username from the user list if they have left the (chat room) so the name doesn't stay on the list indefinately. In your case, you may or may not even need this...depends on whether or not you are implementing some sort of timeout. You probably could modify this to run the the SESSION_OnEnd event in global.asa, but I don't know how reliable the global.asa is, or if you are even using that.
---------------------------------

Sub UpdateList(strName)

arrUsers = Application("Users")
intUserCount = Ubound(arrUsers)
intNewCount = intUserCount
bolUserFound = False

For intCounter = 0 to intUserCount
If Lcase(arrUsers(intCounter, 0)) = Lcase(strName) Then
arrUsers(intCounter, 1) = Now() + 1/1440
End If
Next

For intCounter = 0 to intUserCount
'this checks the timeout, and if it is less than the current time,
'then this name will be removed as it means the user has left
If arrUsers(intCounter, 1) < Now() Then
arrUsers(intCounter, 0) = ""
arrUsers(intCounter, 1) = ""
End If
Next

For intCounter = 0 to intUserCount
If arrUsers(intCounter, 0) = "" Then
intNewCount = intNewCount - 1
End If
Next

ReDim arrTempArray(intNewCount, 1)
intTempCount = 0

For intCounter = 0 to intUserCount
If arrUsers(intCounter, 0) <> "" Then
arrTempArray(intTempCount, 0) = arrUsers(intCounter, 0)
arrTempArray(intTempCount, 1) = arrUsers(intCounter, 1)
intTempCount = intTempCount + 1
End If
Next

APPLICATION.LOCK
Application("Users") = arrTempArray
APPLICATION.UNLOCK

End Sub


---------------------
If you do end up implementing the "timeout" thingy, you will need to call the "UpdateList" sub in each page on your site so that the user timeouts are updated each time they call a page on your site. That way, if the user leaves, their timeout will not be updated and eventually the name will be removed from the list. If you are using global.asa, then you won't need to do that.

Again I apologize for the complexity of this. I am certain there are probably much easier ways of doing this but...well I never figured them out myself LOL So unless someone else gives you a faster and easier way of doing what you want to do, I hope this might help you somewhat...Good luck!

HTH
Frank

rwmoulton
06-24-2003, 12:28 PM
Thanks! I'll dive in and see how it works.

rwmoulton
06-25-2003, 10:24 PM
That looks to be the direction I will head in! Great explanation - thanks for spending the time to do all that!!

I definitely get the gist of what you did and it probably saved me a couple days of solid work on dreaming this.

Frank
06-26-2003, 07:59 PM
Not a problem. Glad to help out. I hope it works out for ya! :)