HI, I am having a few problems, I want to make a simple login that will read a text file where the "username","password" will be, it will be displayed like this:

"username1","password1"
"username2","password2"

etc, I would like it so that the login form will read the data, if a user enters wrong username or wrong pass then they will get a message, so obv if you have two text boxes,

Username[____________] Password[_________________]

username1 would have to match with password1,

Anyway I need the right code to help me do this, as I have create a simple registration process that writes the username/password as above into a text file:

(registration)

Dim savefile As Long
savefile = FreeFile()
Open "I:\new-work\savedfile.txt" For Output As #savefile
Write #savefile, (Text1.Text); (Text2.Text)
Close #savefile

You can try this. but I suggest, if you have several user so you should have a database of username and password. so is better to use SQL or Access to build a database for username and password.
Sadegh.mss
(registration)

Option Explicit
Dim username, password As String

Private Sub ComLogin_Click()
Open "E:\Programming\Sample\file read&write\username.txt" For Input As #1
Input #1, username
Close #1

Open "E:\Programming\Sample\file read&write\password.txt" For Input As #2
Input #2, password
Close #2

If Text1.Text <> username And Text2.Text <> password Then
MsgBox " UserName or Password is wrong"
Else
MsgBox "OK"
End If
End Sub

Private Sub ComSaveSeting_Click()
username = Text1.Text
password = Text2.Text

Open "E:\Programming\Sample\file read&write\username.txt" For Output As #1
Write #1, username
Close #1

Open "E:\Programming\Sample\file read&write\password.txt" For Output As #2
Write #2, password
Close #2
End Sub

HI, I am having a few problems, I want to make a simple login that will read a text file where the "username","password" will be, it will be displayed like this:

"username1","password1"
"username2","password2"

etc, I would like it so that the login form will read the data, if a user enters wrong username or wrong pass then they will get a message.
username1 would have to match with password1,

Anyway I need the right code to help me do this, as I have create a simple registration process that writes the username/password as above into a text file:

try the following code.
i think this code becomes the definite solution to your problem.
just place two textboxes and a button (with default object names) on your form for testing.
plz give me a feedback if this helps you.

place this code under Command1_Click() event and of course create a text file "user.txt" in your project folder with some sample values for testing with this code. the format should be in the following format :-

"username1","password1"
"username2","password2"
.
.
and so on...

Dim uname As String, pwd As String
Dim is_matched As Boolean

If Trim(txtusername.Text) = "" Or Trim(txtpassword.Text) = "" Then
    MsgBox "Missing login info."
    txtusername.SetFocus
    Exit Sub
End If

is_matched = False

Open App.Path & "\user.txt" For Input As #1
    Do While Not EOF(1)
        Input #1, uname, pwd
        If Trim(uname) = Trim(txtusername.Text) And _
            Trim(pwd) = Trim(txtpassword.Text) Then
             is_matched = True
             Close #1
             Exit Do
        End If
    Loop
Close #1

If is_matched = True Then
    MsgBox "Valid login."
Else
    MsgBox "Invalid username or password." & vbCrLf & _
        "Please re-try..."
    txtusername.Text = ""
    txtpassword.Text = ""
    txtusername.SetFocus
End If

regards
Shouvik

How to stop the same username from being written?

when I test it and register same username, it is displayed as,

"carl2k2","mypassword"
"carl2k2","mypassword"

If (Len(txtusername.Text) < 4) Or (Len(txtpassword.Text) < 4) Then
    MsgBox "Minimum length not entered"
    Else
    MsgBox "You have Registered"
   response = MsgBox("Would you like to login now? , Press Cancel to Exit", vbOKCancel, "Note")
     
     If response = vbOK Then 'If user clicks OK then fologin form will display
fologin.Show
frmreg.Visible = False 'after clicking OK then this form will not be visible
     
     
Dim savefile As Long
savefile = FreeFile()
Open "G:\new-work\savedfile.txt" For Append As #savefile
    Write #savefile, (txtusername.Text); (txtpassword.Text)
Close #savefile
Exit Sub


    End If
End If

Would have to read the username line first? and if txtusername == username in txt then should have error message :S?

ok....
try this code.
place two textboxes(txtusername,txtpasswd) and a button. no need to create the text file. it will be automatically created.

Dim user As String, pwd As String
Dim duplicate As Boolean

duplicate = False

If Dir(App.Path & "\user.txt") = "" Then
    GoTo adduser
Else
    Open App.Path & "\user.txt" For Input As #1
        Do While Not EOF(1)
            Input #1, user, pwd
            If Trim(user) = Trim(txtusername.Text) And Trim(pwd) = Trim(txtpasswd.Text) Then
                duplicate = True
                Close #1
                Exit Do
            End If
        Loop
    Close #1

    If duplicate = True Then
        MsgBox "username/password already exists."
    Else
adduser:
        Open App.Path & "\user.txt" For Append As #1
            Write #1, Trim(txtusername.Text), Trim(txtpasswd.Text)
        Close #1
        MsgBox "User created."
    End If

    txtusername.Text = ""
    txtpasswd.Text = ""
    txtusername.SetFocus
End If

give me a feedback.
regards
Shouvik

hey, lol, before reading your message I was thinking that login code you made could work with this right?

anyway this all I came up with so far,

Dim uname As String, pwd As String
Dim is_matched As Boolean

If Trim(frmreg.txtusername.Text) <> Label1.Caption Then
    MsgBox "should work"
    txtusername.SetFocus

Open App.Path & "G:\College Work ------- Updated 2008 March\new-work\savedfile.txt" For Input As #1
    Do While Not EOF(1)
        Input #1, uname
        If Trim(uname) = Trim(frmreg.txtusername.Text) Then
             is_matched = True
             Close #1
             Exit Do
        End If
    Loop
Close #1
    Exit Sub
End If

I coded label1 to = txtusername

so if uname = same as what was writtent then login is already used, although some parts are messy :), but now I read what you put, just wanted to type this if im learning it right


forgot to link it to right form(as this is test form) (txtusername.Text to frmreg.txtusername.Text)

hey Just tried out your code and it still does duplicate username :S


I had to edit some code, because when I use app.data I can't write to file unless I made it an exe,

I changed to this:

If Dir("G:\College Work ------- Updated 2008 March\new-work\test.txt") = "" Then

aha I foudn the problem, if the username and password are the same then it wont write it :D, hopefulyl i can modfy it to work :P so if only username exists


hehe, thanks dude, I just removed the part to check the password, I think you probably did that on purpose :)

I wanted to create a profile page, when the user is logged in their details will be presented,

I can set it so in profile it will display the username just by linking it from a previous form,

I would like to have it so all information from the username will be displayed from the line :S, any way of doing that:P?

I modified the code that you made and added address / postcode, etc

i just can't understand what exactly you are trying to do?

first you posted, you want your program to access a text file where some sort of username and password have been stored. then you will pass a set of username and password from your form's textboxes and your program should check this with corresponding to all the records stored in the text file. now if any of the pairs matches with the textbox values you will give access permission to your user.

secondly you posted, how could you make your program so that when you are creating users it won't accept duplicate username and password. it will only store such a pair which doesn't exist in the textfile.

tell me whether i'm right or wrong?

i have just given you the codes which match your request and i think regarding the issues you posted i gave correct solutions. these codes are tested in my machine and these are working perfectly.

now whatever you said in your last three posts could you please explain it more clearly? i just want a clear explanation in simple and clear english. just tell what exactly you are trying to do?

and one more thing, if its possible plz upload your existing program which you have done so far so that i can test it on my machine and give you most appropriate solutions based on the results generated by your application.

now if you don't wish to do so,
then the summary is -->

i will recommend you to use database instead of using simple textfiles. because storing sensitive data like username and password in such a textfile can be easily viewable by your users. if you use database then your coding becomes more easy and you will get more security and features that you will never get from textfiles.

just think ten times before you post another reply.

ok....

regards
Shouvik

Hi there,

Im trying to make a TXT login form for a VB6 program im making.

I want the TXT to be stored online; e.g http://daniweb.com/LOGIN.TXT

I want the LOGIN.TXT file to contain the simple login credentials.

In my program, the login is the first thing that appears on opening. There are 2 boxes (Text1.text and Text2.text) and a button (Command1)

I tried one of your earlier posted scripts choudhuryshouvi but it didnt login, everytime it said incorrect login when the login was correct. The same script didnt work if the LOGIN.TXT file was stored online.


Can anyone help me??

Thanks
David Rapley

what did you mean by login file stored online?

look, the scripts i have provide here can be applicable on offline textfiles only. if you wish to try to access from an online source then the programming technique is completely different.
now the access technique depends on the type of connectivity you are using. if you use a local area connection network then your login.txt file must be placed into a server and all client nodes must have sufficient priviledge to access the server.
now if you wish to access this file from a remote server such as a webserver you must write an asp script for that. simply writing code from your vb6 application to connect to the text file won't work.

infact, in such a case textfiles are useless. using databases is always preferable in this context.

and one more thing,
all scripts that i provided here are 100% correct and running smoothly. they are all tested. now if any one of these failes to run on your machine that doesn't mean that the code is wrong. you should have to modify the code to suit your program. if its possible for you upload your existing application here so that i can check it on my machine.

by the way, i have checked the link you provided. sorry to say but its a broken link.

now the decision completely depends upto you. but you must not blame on code/scripts submitted by anyone of the members.

regards
Shouvik

Hi again,


The link was broken because it was just an example, all URL's i use are examples

Your script did work. Sorry!


Ive sorted out the login for my program.

The login works if LOGIN.TXT is in the same directory as the program.

But i dont want to include LOGIN.TXT along with the program, as it wont be up to date with new logins.

So how can i make the VB6 login to get the data from the internet.

Open App.Path & "\LOGIN.txt" For Input As #1
Do While Not EOF(1)
Input #1, uname, pwd
If Trim(uname) = Trim(txtusername.Text) And _
Trim(pwd) = Trim(txtpassword.Text) Then
is_matched = True
Close #1
Exit Do
End If
Loop
Close #1

PART 1
As you can see, it opens finds LOGIN.TXT in the App.Path. When i change "\LOGIN.txt" to "http://mywebsite.com/LOGIN.txt" it doesnt find the file.
So, i change it to the web one, and then export the EXE, then open it and it doesnt work. Obviously because it cant find the TXT file.

How do i need to change this line;

Open App.Path & "\LOGIN.txt" For Input As #1

Its this part which is the problem;

Open App.Path & "\LOGIN.txt"

It doesnt work when i change it to;

Open App.Path & "http://mywebsite.com/LOGIN.txt"

SO what do i need the line to???


I posted this too fast, i didnt read your last post. So sorry for repeating. So how would i make a ASP script for what i want to achieve. I really need this login script to work the way i need it to, otherwise anyone can use my program. I cant find anything online, and i know someone who has done it, but they wont tell me :( .


Thanks in advance

David Rapley

obviously it won't work.
the reason is, when you are specifying the location App.Path & "http://mywebsite.com/LOGIN.txt, it searches for the login.txt file inside a directory http://mywebsite.com inside the project directory. but there is no such thing exists. so it is returning you an error.

how could the program recognize that the specified location is a virtual directory until and unless you tell it so? for this you need to do coding. this is not recommended using vb6. instead of this use an ASP script with VBSCRIPT coding.

You can visit this link for accessing files using ASP scripts

regards
Shouvik

Thanks for the info but he did made part of a VB6 login which interacts with a txt file on his web server. Im currently trying to rip this so i can make it work on my own server.

Thanks anyway. :)

and btw, i wouldnt have gone into coding, because its actually MUCH simpler. I read his code, and its easy :D

Bye

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.