Skip to main content
All CollectionsWebsiteWebsite Development
How to Add a Login Form to a Website
How to Add a Login Form to a Website

Requiring visitors to log in before accessing the website

Updated over a year ago

If you want to have more control over who reaches your website's content, you can implement a login form. This way, a password will be required to provide access to your website or parts of it.

Let’s go through different ways how it can be achieved:

Option 1 – Password Protect Directories

If you use Hostinger Website Builder, refer to this article instead: Website Builder: How to Password-Protect a Page 💡

One of the easiest ways is to enable the Password Protect Directories feature for your website.

This option allows you to protect all of your website or just one directory without the need to edit any code 😊

Option 2 – WordPress Plugin

This option is not available if you use Hostinger Website Builder 💡

If your website is WordPress-based, you can use a plugin such as WordFence to enable two-factor authentication (2FA) or add a captcha feature to prevent bots from attempting to log in to your website.

There are other plugins that can help you limit access to registered users only, such as Force Login.

Option 3 – .htaccess

This option is not available if you use Hostinger Website Builder 💡

If you feel comfortable coding, you can add the login form by a .htaccess file in the directory you want to protect.

First, check if the directory you want to protect has a .htaccess file, or you can create one if needed. For example, to protect your complete website, place it in the public_html folder.

You will also need to create a file named .htpasswd that can be anywhere on your account. For safety reasons, it is recommended that the .htaccess and .htpasswd files be stored in different folders.

Once you have created the .htpasswd file, add to it the following line:

username:password

Replacing username and password with your desired values.

Go back to the .htaccess file and insert these lines:

AuthType Basic
AuthName "Authorization required"
AuthUserFile /path/to/.htpasswd
require valid-user

Where /path/to/.htpasswd is the actual path where the .htpasswd file will be located. For your Hostinger account, if the file is inside a subfolder of your domain, it would look similar to this:

/home/u123456789/domains/domain.tld/public_html/subfolder/.htpasswd

That's it! Your website will be password protected 😊

Did this answer your question?