|
-
Dec 1st, 2001, 01:32 AM
#1
Thread Starter
Lively Member
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.
-
Dec 1st, 2001, 01:35 AM
#2
PowerPoster
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:
If Gotten_Passwork = File_Password Then
'the password is right
Else
'the password is wrong
End If
-
Dec 1st, 2001, 01:52 AM
#3
Thread Starter
Lively Member
I'm just having problems getting the password and username from the textfile I guess.. Heres the code I have so far....
VB Code:
Public Sub Verify()
Dim compare As Integer
Position = 0
Position = InStr(Position + 1, Form1.RichTextBox1.Text, user, vbTextCompare)
If Position > 0 Then
Form1.RichTextBox1.SelStart = Position - 1
Form1.RichTextBox1.SelLength = Len(user)
Verifypass
Else
denylogin
End If
End Sub
-
Dec 1st, 2001, 01:55 AM
#4
PowerPoster
how are the usernames and passwords put in the file? How does the file look like?
-
Dec 1st, 2001, 02:02 AM
#5
Thread Starter
Lively Member
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
-
Dec 1st, 2001, 02:42 AM
#6
PowerPoster
I first suggest that you get all the usernames and passwords in an array using the following function:
VB Code:
Public Function GetAccounts(users(), passwords())
Dim counter As Integer
Dim mystr As String
Open "myfile.txt" For Input As #1
Do
Line Input #1, mystr
If Left$(mystr, 7) = "ACCOUNT" Then
counter = counter + 1
ReDim Preserve users(counter - 1)
ReDim Preserve passwords(counter - 1)
ElseIf Left$(mystr, 4) = "NAME" Then
users(counter - 1) = Trim(Mid$(mystr, 5))
ElseIf Left$(mystr, 4) = "PASS" Then
passwords(counter - 1) = Trim(Mid$(mystr, 5))
End If
Loop Until EOF(1)
Close #1
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:
Dim users(), passwords()
GetAccounts users(), passwords()
For i = 0 To UBOUND(users)
If (txtuser.Text = users(i)) And (txtpass.Text = passwords(i)) Then
'the account is valid
Else
'the account is invalid
End If
Next
Hope that helps
-
Dec 1st, 2001, 03:03 AM
#7
Thread Starter
Lively Member
VB Code:
Public Function GetAccounts(users(), passwords())
Dim counter As Integer
Dim mystr As String
Open "C:\UOEMUD\accounts.txt" For Input As #1
Do
Line Input #1, mystr
If Left$(mystr, 7) = "ACCOUNT" Then
counter = counter + 1
ReDim Preserve users(counter - 1)
ReDim Preserve passwords(counter - 1)
ElseIf Left$(mystr, 4) = "NAME" Then
users(counter - 1) = Trim(Mid$(mystr, 5))
ElseIf Left$(mystr, 4) = "PASS" Then
passwords(counter - 1) = Trim(Mid$(mystr, 5))
End If
Loop Until EOF(1)
Close #1
End Function
Public Sub verifyuser()
GetAccounts users(), passwords()
For i = 0 To UBound(users)
If (user = users(i)) And (password = passwords(i)) Then
SendServerList
Else
denylogin
End If
Next
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.
-
Dec 1st, 2001, 04:00 AM
#8
PowerPoster
Hi
Too much code for me to wade thru so here is a method using Random Access Files.
regards
Stuart
VB Code:
Option Explicit
Private Type UserType
Account As Long
Name As String * 30
Password As String * 8
End Type
Dim User As UserType
Dim lCounter As Long
Dim lFileNum As Byte
Private Sub Command1_Click()
'This routine is just creating a sample text file
'U will create this in ur own routines
lFileNum = FreeFile
Open "C:\passwords.txt" For Random As lFileNum Len = Len(User)
For lCounter = 1 To 9 'Create 9 users
With User
.Account = lCounter
.Name = "User Number " & lCounter
.Password = String(8, CStr(lCounter))
End With
Put lFileNum, lCounter, User 'Store in file
Next
Close lFileNum
End Sub
Private Sub Command2_Click()
Dim lUserTest As String
Dim lPassTest As String
Dim lFound As Boolean
lUserTest = "User Number 3" 'U can get this from a textbox or whatever
lPassTest = "33333333" 'Same again
lFound = False 'Check if user found
lFileNum = FreeFile
Open "C:\passwords.txt" For Random As lFileNum Len = Len(User)
For lCounter = 1 To FileLen("C:\passwords.txt") \ Len(User)
Get lFileNum, lCounter, User 'Read line
With User
If UCase(Trim$(.Name)) = UCase(lUserTest) Then 'Check user name
lFound = True 'User found
If UCase(Trim$(.Password)) = UCase(lPassTest) Then 'Check password
Debug.Print .Account, .Name, .Password 'Found.. do stuff
Exit For
Else
Debug.Print "wrong password" 'wrong password.. u may want to allow more tries
End If
End If
End With
Next
Close lFileNum
If Not lFound Then Debug.Print "user not found" 'No user found
End Sub
-
Dec 1st, 2001, 09:22 AM
#9
PowerPoster
Originally posted by nemesys777
VB Code:
Public Function GetAccounts(users(), passwords())
Dim counter As Integer
Dim mystr As String
Open "C:\UOEMUD\accounts.txt" For Input As #1
Do
Line Input #1, mystr
If Left$(mystr, 7) = "ACCOUNT" Then
counter = counter + 1
ReDim Preserve users(counter - 1)
ReDim Preserve passwords(counter - 1)
ElseIf Left$(mystr, 4) = "NAME" Then
users(counter - 1) = Trim(Mid$(mystr, 5))
ElseIf Left$(mystr, 4) = "PASS" Then
passwords(counter - 1) = Trim(Mid$(mystr, 5))
End If
Loop Until EOF(1)
Close #1
End Function
Public Sub verifyuser()
GetAccounts users(), passwords()
For i = 0 To UBound(users)
If (user = users(i)) And (password = passwords(i)) Then
SendServerList
Else
denylogin
End If
Next
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:
Dim users(), passwords()
Public Function GetAccounts(users(), passwords())
Dim counter As Integer
Dim mystr As String
Open "C:\hi.txt" For Input As #1
Do
Line Input #1, mystr
If Left$(mystr, 7) = "ACCOUNT" Then
counter = counter + 1
ReDim Preserve users(counter - 1)
ReDim Preserve passwords(counter - 1)
ElseIf Left$(mystr, 4) = "NAME" Then
users(counter - 1) = Trim(Mid$(mystr, 5))
ElseIf Left$(mystr, 4) = "PASS" Then
passwords(counter - 1) = Trim(Mid$(mystr, 5))
End If
Loop Until EOF(1)
Close #1
End Function
Public Sub verifyuser()
GetAccounts users(), passwords()
For i = 0 To UBound(users)
If (user = users(i)) And (password = passwords(i)) Then
MsgBox "SendServerList"
Exit For
Else
MsgBox "denylogin"
End If
Next
End Sub
Private Sub Command1_Click()
verifyuser
End Sub
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|