Navigation: For Developers > Extend Functionality with HEScript > Sample Scripts >

How to password protect pages?





📚 This guide on how to utilize security profiles and scripting in HTML Executable. This guide will walk you through a practical example, demonstrating how to prompt users for a password to access certain pages, cache the password, and use a security profile to protect HTML pages with this password. Let's dive in!


The Objective 🎯


Our script aims to:


ØPrompt the user to provide a password to view certain pages.

ØCache the password so it is only prompted once.

ØUse a security profile to mark which HTML pages should be protected using this password.


The outcome? If a user attempts to view our password-protected pages, they will be prompted for the password. If the correct password is provided, access to the pages is granted. If not, an error page is displayed.


Step-by-Step Guide 📝


Step 1: Writing the Script 🖋️


We'll start by writing a Boolean function that can be used as a condition for our security profile. Head over to the Script Manager, create a blank script named "MySecurity1", and copy/paste the following code:


// MySecurity1
// A script to password protect pages.
// Our secret password:
function CheckSecurity1: Boolean;
 S: String;
{ 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.
If it returns False, then the secure pages may be displayed. }
// 1) Check if the password was cached.
   S := GetGlobalVar("CachePassword1", "");
   if S="" then
//  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", "");
// 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.


This code provides us with a Boolean function that we can use in a security profile. Don't forget to save the script!


Step 2: Creating a Security Profile 🛡️


Next, we'll create a security profile:


ØGo to the Security ribbon and select "Security Profiles".

ØClick "Add" and select "Add New Security Profile".

ØEnter "Secure pages 1" as the profile's name.




ØClick "Add" and select "Add new condition".

ØClick on "If the following boolean script function returns true" and select "checksecurity1":





ØFinally, click “Configure” (select the condition in order to enable the Configure button). In the “Actions to execute” window, select the “Locking Pages” tab. Then enable “Pages are locked and cannot be viewed”:



Your security profile is now ready!



Step 3: Protecting Your Pages 📄


Now, let's select the pages we want to protect:


ØGo to the File Manager and select the page(s) you want to protect.

ØClick on "Properties" and in the File Properties window, select "Security".

ØIn the "Selected Security Profile" list, look for the security profile we just created, i.e. "Secure Pages 1".


Final Step: Test Your Publication 🧪


You can now compile your publication and test it.


Notes 📝


Feel free to customize this example:

ØInstead of using a prompt dialog box, you could create a login system.

ØYou can use anything else than a password to protect your files. Just modify the Boolean function.