Hi I'm trying to add a login screen and user accounts to my program.
Basically the variables for the username and password come from an ini file where the variables are defined. This is so the user can edit their username or password. When the user types in a username that is also in the ini file and a password that is also in the ini they will log in.
However when I run my program and the user enters nothing in the Username or Password edit boxes they are logged in. When they enter the correct username and password are defined in the ini files or if they enter anything else they cant log in. Can someone help me I think its my code
Heres the code for the ini File. Its stored in the same folder as the program and is called Password.ini
Password.ini:
Heres the code for the program. The parts in bold are the main lines relating to the either the ini File or the user and pass variables.[Users]
Name=Alex
[Pass]
NPass=homepage
Any help at all would be great. Thanksunit Password;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, iniFiles;
//uses iniFiles. The application uses iniFiles as part of the code.
type
TfrmPassword = class(TForm)
ledtUsername: TLabeledEdit;
ledtPassword: TLabeledEdit;
btnOK: TButton;
btnCancel: TButton;
procedure FormCreate(Sender: TObject);
procedure btnOKClick(Sender: TObject);
procedure btnCancelClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
frmPassword: TfrmPassword;
IniFile : TIniFile; //Defining IniFile as variable TIniFile.
User : String; //Defining User as a string.
Pass : String; //Defining Pass as a string.[/B]
implementation
uses Welcome;
{$R *.dfm}
procedure TfrmPassword.FormCreate(Sender: TObject);
begin
IniFile := TIniFile.Create(ChangeFileExt(Application.ExeName, '.ini'));
//Associates the ini File 'Password' with the application.
User := IniFile.ReadString('Users','Name','');
//User equals the variable Name in the users section of the ini file.
Pass := IniFile.ReadString('Pass','NPass','');
//Pass equals the variable NPass in the Pass section of the ini file.
end;
procedure TfrmPassword.btnOKClick(Sender: TObject);
begin
if ledtUsername.Text = User
then
if ledtPassword.Text = Pass
then
frmWelcome.show
else
ShowMessage ('Invalid Password.')
else
ShowMessage ('Invalid Username.');
end;
procedure TfrmPassword.btnCancelClick(Sender: TObject);
begin
close;
end;![]()
First thing I would do: showmessage(User) and showmessage(Pass) after reading in the values.
Tried that, still have the same problem![]()
Well, are they set to the correct values? The issue is to isolate whether you are not reading the values from the ini file (which I suspect) or if you're not comparing them properly. If the issue is reading the values, then you need to look at whether the ini file is formatted correctly.
You can always add watches or set a bread point, move your mouse over the variable when it's break will show the value.
Thanks
Patrick Ooi, http://www.webhyper.com
I think that storing username and password in an ini file is a bad Idea to start with. One other problem I can see is that you will only be able to have one user at a time do to the layout and names of the variables in the ini file. if you don't want a standard SQL database or .mdb file with ADO Components try TuboPower Components B-TreeFiler.
All looks good.
Try to manually check variables:
ledtUsername.Text
ledtPassword.Text
Also try to use Trim function.
Try:
//Associates the ini File 'Password' with the application.
User := IniFile.ReadString('Users','Name','This isn''t real');
//User equals the variable Name in the users section of the ini file.
Pass := IniFile.ReadString('Pass','NPass','This is also bogus');
Then, if the ini isn't working, your values will make it so you can't log in unless you enter user "This isn't real" and password "This is also bogus".
Also, you didn't state if you could log in using the defined user and pass, which I very strongly suspect you could not.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks