Almost every application system has corresponding permission management functions. For large multi-user application systems, permission settings are generally saved in the database. However, for small single-user application systems and non-database application systems, it is not necessary to save permission settings in the database, but using files to save is not convenient and unsafe, because files are likely to be deleted or destroyed. In fact, flexibly leveraging the features of the Windows registry can easily set usernames and passwords for the application system. This method is not only simple to program, but also has high security. Here is an example that this routine is debugged and passed in Windows 98 and Delphi4 environments.
The design process is: first determine whether it is the first time to use it. If it is the first time to use it, a new username and password is required to be set, and the system saves the username and password; if it is not the first time to use it, a username and password are required to log in.
In the Delphi development environment, create a new project Project1, add a form Form1 to Project1; add two buttons to Form1, one for sure and the other for cancel; then add two Text boxes EditUser and EditPass to Form1, respectively, for username and password input boxes. Define the form-level global variable RegF:Tregistry. And add Registry to the Uses of the interface.
Add the function Wsz_IfFirst to determine whether it is the first use; add the process Wsz_SaveUse to save the user name and password; add the function Wsz_CheckUser to determine whether the user name and password are correct. Then, in the FormShow event and the Determining button event of Form1, these three functions and processes are called separately to meet the design requirements.
//Discern whether it is the first time to use it
function Wsz_IfFirst:Boolean;
var
sUser:string;
sPass:string;
begin
result := false;
RegF:=;
:=HKEY_LOCAL_MACHINE;
("SOFTWARE\MicroSoft\whh726",TRUE);
sUser := ("user");
sPass := ("pass");
if ((length(sUser)<=0) and (length(sPass)<=0)) then
result := true ;
;
end;
//Judge whether the user name and password are correct
function Wsz_CheckUser(MyUser:string;MyPass :string):Boolean;
var
sUser:string;
sPass:string;
begin
result:= false;
RegF:=;
:=HKEY_LOCAL_MACHINE;
("SOFTWARE\MicroSoft\whh726",TRUE);
sUser := ("user");
sPass := ("pass");
if (sUser=MyUser) and (sPass=MyPass) then
result := true;
;
end;
//Save username and password
procedure Wsz_SaveUser(MyUser:string;MyPass :string);
begin
RegF:=;
:=HKEY_LOCAL_MACHINE;
("SOFTWARE\MicroSoft\whh726",TRUE);
("user",MyUser);
("pass",MyPass);
;
end;
//Cancel button event
procedure (Sender: Tobject);
begin
Close;
end;
//Confirm the button event
procedure (Sender: Tobject);
var
suser:string;
spass:string;
begin
suser := trim();
spass := trim();
if (length(suser)<=0) or (length(spass)<=0) then
begin
("Username and password must be entered!", "System prompt",
MB_OK + MB_ICONINFORMATION);
;
exit;
end;
if Wsz_IfFirst then
begin
//Processing the username and password for the first login
if (
"This is your first time using this software. Please remember your username and password so that you can log in next time. Are you entering the system now?",
"System prompt",
MB_OKCANCEL + MB_ICONQUESTION) = IDOK then
begin
//save
Wsz_SaveUser(suser,spass);
end
else
begin
;
exit;
end;
end else
begin
if not Wsz_CheckUser(suser,spass) then
begin
("Sorry, the username and password are entered incorrectly!",
"System prompt",
MB_OK + MB_ICONINFORMATION);
;
exit;
end;
end;
//Global variables
GB_Wsz_User := suser;
GB_Wsz_PASS := spass;
Close;
end;
//The Show event of the form
procedure (Sender: Tobject);
begin
if Wsz_IfFirst then
begin
(
"This is your first time using this software, you can enter your username and password at will."
"System prompt",
MB_OK + MB_ICONINFORMATION);
end;
end;