Results 1 to 9 of 9

Thread: How to compare one string to.....HELP!!

  1. #1

    Thread Starter
    Lively Member nemesys777's Avatar
    Join Date
    Nov 2001
    Posts
    103

    Angry How to compare one string to.....HELP!!

    Hello,
    I'm trying to compare 2 strings(username, and password) to a textfie that I have loaded into my app, via richtextbox.
    If there is a way to compare it without using the richtextbox that is cool. I have a textfile that has the usernames and accounts of my users in it. I get the user name and pass from the client they are using(winsock)..then I want to check if the username and password exists in my textfile. I thought I had this down, but it's not comparing right. Anyone know how to do this??? Thanks.

  2. #2
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827
    What code are you using and which thing are you getting wrong(how come they are not comparing right)?

    You just compare like

    VB Code:
    1. If Gotten_Passwork = File_Password Then
    2. 'the password is right
    3. Else
    4. 'the password is wrong
    5. End If
    Baaaaaaaaah

  3. #3

    Thread Starter
    Lively Member nemesys777's Avatar
    Join Date
    Nov 2001
    Posts
    103
    I'm just having problems getting the password and username from the textfile I guess.. Heres the code I have so far....

    VB Code:
    1. Public Sub Verify()
    2. Dim compare As Integer
    3. Position = 0
    4. Position = InStr(Position + 1, Form1.RichTextBox1.Text, user, vbTextCompare)
    5. If Position > 0 Then
    6. Form1.RichTextBox1.SelStart = Position - 1
    7. Form1.RichTextBox1.SelLength = Len(user)
    8. Verifypass
    9. Else
    10. denylogin
    11. End If
    12. End Sub

  4. #4
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827
    how are the usernames and passwords put in the file? How does the file look like?
    Baaaaaaaaah

  5. #5

    Thread Starter
    Lively Member nemesys777's Avatar
    Join Date
    Nov 2001
    Posts
    103
    ACCOUNT 0
    NAME user1
    PASS magic

    ACCOUNT 1
    NAME user2
    PASS xpisevil

    Like that--pretty simple. I was loading all the text from my file into a richtextbox for faster access, but I dont need to. I just need to take a string(inputted user name) and compare it to the accounts file... If the username is found..then compare if password string.
    If both are good then grantaccess else denylogin

  6. #6
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827
    I first suggest that you get all the usernames and passwords in an array using the following function:

    VB Code:
    1. Public Function GetAccounts(users(), passwords())
    2. Dim counter As Integer
    3. Dim mystr As String
    4.  
    5. Open "myfile.txt" For Input As #1
    6. Do
    7. Line Input #1, mystr
    8. If Left$(mystr, 7) = "ACCOUNT" Then
    9. counter = counter + 1
    10. ReDim Preserve users(counter - 1)
    11. ReDim Preserve passwords(counter - 1)
    12. ElseIf Left$(mystr, 4) = "NAME" Then
    13. users(counter - 1) = Trim(Mid$(mystr, 5))
    14. ElseIf Left$(mystr, 4) = "PASS" Then
    15. passwords(counter - 1) = Trim(Mid$(mystr, 5))
    16. End If
    17. Loop Until EOF(1)
    18. Close #1
    19. End Function
    (The file format should be the same how you described it)

    Now, get the username and password from the user and compare to all the items in the array like this:

    VB Code:
    1. Dim users(), passwords()
    2. GetAccounts users(), passwords()
    3. For i = 0 To UBOUND(users)
    4. If (txtuser.Text = users(i)) And (txtpass.Text = passwords(i)) Then
    5. 'the account is valid
    6. Else
    7. 'the account is invalid
    8. End If
    9. Next

    Hope that helps
    Baaaaaaaaah

  7. #7

    Thread Starter
    Lively Member nemesys777's Avatar
    Join Date
    Nov 2001
    Posts
    103
    VB Code:
    1. Public Function GetAccounts(users(), passwords())
    2. Dim counter As Integer
    3. Dim mystr As String
    4.  
    5. Open "C:\UOEMUD\accounts.txt" For Input As #1
    6. Do
    7. Line Input #1, mystr
    8. If Left$(mystr, 7) = "ACCOUNT" Then
    9. counter = counter + 1
    10. ReDim Preserve users(counter - 1)
    11. ReDim Preserve passwords(counter - 1)
    12. ElseIf Left$(mystr, 4) = "NAME" Then
    13. users(counter - 1) = Trim(Mid$(mystr, 5))
    14. ElseIf Left$(mystr, 4) = "PASS" Then
    15. passwords(counter - 1) = Trim(Mid$(mystr, 5))
    16. End If
    17. Loop Until EOF(1)
    18. Close #1
    19. End Function
    20.  
    21. Public Sub verifyuser()
    22. GetAccounts users(), passwords()
    23. For i = 0 To UBound(users)
    24. If (user = users(i)) And (password = passwords(i)) Then
    25. SendServerList
    26. Else
    27. denylogin
    28. End If
    29. Next
    30. End Sub
    I tried all this and I dont get any errors, but it still deny's access no matter what account or password I put in..eans it's not working.

  8. #8
    PowerPoster beachbum's Avatar
    Join Date
    Jul 2001
    Location
    Wollongong, NSW, Australia
    Posts
    2,274
    Hi
    Too much code for me to wade thru so here is a method using Random Access Files.
    regards
    Stuart
    VB Code:
    1. Option Explicit
    2.  
    3. Private Type UserType
    4.     Account As Long
    5.     Name As String * 30
    6.     Password As String * 8
    7. End Type
    8. Dim User As UserType
    9. Dim lCounter As Long
    10. Dim lFileNum As Byte
    11.  
    12. Private Sub Command1_Click()
    13. 'This routine is just creating a sample text file
    14. 'U will create this in ur own routines
    15.     lFileNum = FreeFile
    16.     Open "C:\passwords.txt" For Random As lFileNum Len = Len(User)
    17.     For lCounter = 1 To 9 'Create 9 users
    18.         With User
    19.             .Account = lCounter
    20.             .Name = "User Number " & lCounter
    21.             .Password = String(8, CStr(lCounter))
    22.         End With
    23.         Put lFileNum, lCounter, User 'Store in file
    24.     Next
    25.     Close lFileNum
    26. End Sub
    27.  
    28. Private Sub Command2_Click()
    29.     Dim lUserTest As String
    30.     Dim lPassTest As String
    31.     Dim lFound As Boolean
    32.    
    33.     lUserTest = "User Number 3" 'U can get this from a textbox or whatever
    34.     lPassTest = "33333333" 'Same again
    35.     lFound = False 'Check if user found
    36.    
    37.     lFileNum = FreeFile
    38.     Open "C:\passwords.txt" For Random As lFileNum Len = Len(User)
    39.     For lCounter = 1 To FileLen("C:\passwords.txt") \ Len(User)
    40.         Get lFileNum, lCounter, User 'Read line
    41.         With User
    42.             If UCase(Trim$(.Name)) = UCase(lUserTest) Then 'Check user name
    43.                 lFound = True 'User found
    44.                 If UCase(Trim$(.Password)) = UCase(lPassTest) Then 'Check password
    45.                     Debug.Print .Account, .Name, .Password 'Found.. do stuff
    46.                     Exit For
    47.                 Else
    48.                     Debug.Print "wrong password" 'wrong password.. u may want to allow more tries
    49.                 End If
    50.             End If
    51.         End With
    52.     Next
    53.     Close lFileNum
    54.    
    55.     If Not lFound Then Debug.Print "user not found" 'No user found
    56. End Sub
    Stuart Laidlaw
    Brightspark Financial Software
    http://www.gstsmartbook.com

  9. #9
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827
    Originally posted by nemesys777
    VB Code:
    1. Public Function GetAccounts(users(), passwords())
    2. Dim counter As Integer
    3. Dim mystr As String
    4.  
    5. Open "C:\UOEMUD\accounts.txt" For Input As #1
    6. Do
    7. Line Input #1, mystr
    8. If Left$(mystr, 7) = "ACCOUNT" Then
    9. counter = counter + 1
    10. ReDim Preserve users(counter - 1)
    11. ReDim Preserve passwords(counter - 1)
    12. ElseIf Left$(mystr, 4) = "NAME" Then
    13. users(counter - 1) = Trim(Mid$(mystr, 5))
    14. ElseIf Left$(mystr, 4) = "PASS" Then
    15. passwords(counter - 1) = Trim(Mid$(mystr, 5))
    16. End If
    17. Loop Until EOF(1)
    18. Close #1
    19. End Function
    20.  
    21. Public Sub verifyuser()
    22. GetAccounts users(), passwords()
    23. For i = 0 To UBound(users)
    24. If (user = users(i)) And (password = passwords(i)) Then
    25. SendServerList
    26. Else
    27. denylogin
    28. End If
    29. Next
    30. End Sub
    I tried all this and I dont get any errors, but it still deny's access no matter what account or password I put in..eans it's not working.
    It works fine for me
    Here is a complete sample code to test it (you have 1 command button, 2 text boxes named "user" and "password" on the form):

    VB Code:
    1. Dim users(), passwords()
    2. Public Function GetAccounts(users(), passwords())
    3. Dim counter As Integer
    4. Dim mystr As String
    5.  
    6. Open "C:\hi.txt" For Input As #1
    7. Do
    8. Line Input #1, mystr
    9. If Left$(mystr, 7) = "ACCOUNT" Then
    10. counter = counter + 1
    11. ReDim Preserve users(counter - 1)
    12. ReDim Preserve passwords(counter - 1)
    13. ElseIf Left$(mystr, 4) = "NAME" Then
    14. users(counter - 1) = Trim(Mid$(mystr, 5))
    15. ElseIf Left$(mystr, 4) = "PASS" Then
    16. passwords(counter - 1) = Trim(Mid$(mystr, 5))
    17. End If
    18. Loop Until EOF(1)
    19. Close #1
    20. End Function
    21.  
    22. Public Sub verifyuser()
    23. GetAccounts users(), passwords()
    24. For i = 0 To UBound(users)
    25. If (user = users(i)) And (password = passwords(i)) Then
    26. MsgBox "SendServerList"
    27. Exit For
    28. Else
    29. MsgBox "denylogin"
    30. End If
    31. Next
    32. End Sub
    33.  
    34. Private Sub Command1_Click()
    35. verifyuser
    36. End Sub
    Baaaaaaaaah

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width