How to password protect pages? |
This detailed sample is really interesting because it shows you how to use security profiles in addition to scripting. You moreover have a live demonstration below. The goal of this script is
The result:
StepsTo cache the password, we will use a persistent global variable name "CachePassword1". The secret password is "reality".
Since we use a security profile, we will write a Boolean function that can be used as a condition for our security profile.
// MySecurity1
// A script to password protect pages.
// Our secret password:
const
OurPassword="reality";
function CheckSecurity1: Boolean;
var
S: String;
begin
{ This function is called by the security profile.
If it returns True, we configure the actions so that
the user may NOT view the pages.
It if returns False, then the secure pages may be displayed. }
// 1) Check if the password was cached.
S := GetGlobalVar("CachePassword1", "");
if S="" then
begin
// The password was never asked.
// So we ask it.
S := InputBox("Password protected page"#13#10"This page is password protected; please enter the password in order to continue", "Security", "");
end;
// 2) Check whether the password is correct.
Result := S <> OurPassword;
// Cache the password if valid.
if not Result then SetGlobalVar("CachePassword1", S, True);
// Result = true => pages locked.
// Result = false => pages unlocked.
end;Thanks to this code, we have a Boolean function that we can use in a security profile.
You can now compile your publication and test it. Live demonstrationDo you want to test this sample? The following page was password protected using the same way. Try to use a wrong password and then use the same password as above to access to it. If you enter a wrong password, you will be redirected to a custom error page (use the Locking Pages option above to do that). Note: By default the password is cached on your system. To clear the password and try this sample again, just click on this button: NotesYou can of course customize this example:
|