How to Password Protect a Web Page (Secure Methods + Simple Scripts)
Need to password protect a web page so only certain people can see it? Back in 2005 we shared two simple JavaScript scripts to do it. They still work as a light privacy screen, but anyone who views the page source can read the password. In 2026 there are much safer and still easy ways. Here’s how to password protect a web page properly, plus the original scripts (updated) if you just need a quick, casual lock.

Quick answer: On WordPress, set the page’s Visibility to Password protected. On a regular website, use your hosting panel’s Password Protect Directories option (or an .htaccess and .htpasswd file), which makes the server ask for a username and password before showing anything. Avoid JavaScript-only passwords for anything private.
Table of Contents
Secure ways to password protect a web page
1. WordPress password protected pages
In the WordPress editor, open the Visibility setting (in the Publish box or the post sidebar), choose Password protected, type a password and update. Visitors see a password form instead of the content. WordPress explains this in its content visibility guide. For stronger control, a membership or private site plugin can require users to log in.
2. Your hosting control panel
Most web hosts can protect a folder without any code. In cPanel look for Directory Privacy; on other panels (including Hostinger’s hPanel) look for Password Protect Directories. Pick the folder, add a username and password, and the server will block everyone else.
3. .htaccess and .htpasswd (Apache and LiteSpeed servers)
This is the classic server-side method, called HTTP Basic Authentication. Create a password file outside your public folder, for example with this command on the server:
htpasswd -c /home/user/.htpasswd yournameThen add this to the .htaccess file in the folder you want to protect:
AuthType Basic
AuthName "Private area"
AuthUserFile /home/user/.htpasswd
Require valid-userThe browser now shows a login box before loading any file in that folder. Always use HTTPS so the password isn’t sent in plain text. If you’re new to .htaccess, see our guide to .htaccess rules for 410 error pages for how the file works.
4. Nginx servers
On Nginx, the same idea uses the auth_basic and auth_basic_user_file settings inside a location block, as described in the Nginx documentation.
5. Cloudflare Access
If your site uses Cloudflare, Cloudflare Access can put a login page (for example, a one-time code sent to approved email addresses) in front of any page or folder. It’s free for small teams and doesn’t need any code on your site.
6. Website builders and static hosts
Many site builders and static hosting services include a page password setting, sometimes only on paid plans. Check your platform’s settings for “password protection” or “private page”.
Simple JavaScript password scripts (not secure)
These are our original 2005 scripts, cleaned up for modern browsers. They’re fine for hiding a fun surprise page or a draft from casual visitors, but remember:
- Anyone can view the page source and read the password.
- The protected page itself isn’t hidden. If someone knows its address, they can open it directly.
- They don’t work if JavaScript is turned off.
Never use them for personal data, client files or anything that must stay private.
Password script 1: username and password form
This shows a small login form. If the username and password match, the visitor is sent to the private page. Replace SECRET_USER, SECRET_PASSWORD and the page address with your own.
<form onsubmit="return checkLogin(this)">
Username: <input name="user" type="text">
Password: <input name="pass" type="password">
<button type="submit">Login</button>
</form>
<script>
function checkLogin(form) {
if (form.user.value === "SECRET_USER" && form.pass.value === "SECRET_PASSWORD") {
window.location.href = "https://YOURDOMAIN.com/private-page.html";
} else {
alert("Wrong username or password");
}
return false;
}
</script>Password script 2: password prompt on page load
Put this in the <head> of the page you want to lock. It asks for a password when the page opens and sends everyone with the wrong password to another page.
<script>
var password = prompt("Enter password");
if (password !== "SECRET_PASSWORD") {
window.location.href = "https://YOURDOMAIN.com/";
}
</script>Frequently asked questions
Can I password protect a web page without a server?
Only weakly. JavaScript passwords run in the visitor’s browser, so they can always be bypassed. For real protection you need the server, WordPress or a service like Cloudflare Access to check the password.
Is a password protected page hidden from Google?
Pages behind server-side or WordPress passwords can’t be read by Google, so their content won’t appear in search. Pages “protected” only by JavaScript can still be found and indexed.
How do I password protect a page on Google Sites or Blogger?
Neither offers per-page passwords. Instead, share the whole site or blog only with chosen Google accounts in its sharing or permissions settings.







I put the script to protect a page of my website, but now when I go to the editor page, it doesn’t allow me to edit anything, it redirects me to the non-edit version of the page. Any suggestions as to how to remove the code?
Re comments by Fledermaus
all javascript code can be saved in a separate *.js file and link it to html page via
does this clear up ur concern?
hey ur not being rude… brain teaser maybe…
take all the spaces out of anything that is not inside tags and it will work anywhere within the body tags of your webpage
So, could someone please show the full working script and kindly explain exactly where it needs to slip on. Am so confused… Thanks.
Hey FAO, thank you for your tips.
Finally i get it to work.
TQ all
The second one works although it took a bit of fiddling about with. I’m certainly no expert in website programming however for anyone else who is struggling I will try and give a slightly clearer description. You need to take out the first triangle bracket and exclamation mark and two dashes and space at the beginning of the second line just before the word var. Then you also need to remove the two dashes and triangle bracket on the penultimate line. Leave in the forward slashes at the end and the script, and other bits. This worked for me and I hope it helps.
FAO
I know how to paste it in – that’s fine.
I’ve followed every tip or trick here. No dice.
It simply does not work. I have the nice little code in between the tags and all but it simply doesn’t have any effect. I just need something simple so I can break up with Typepad and go to Blogspot without the extended family giving me a hard time. They feel better with a “password entry” blog.
I am having a similar problem to Rob, I have used the second code… it is Simple & Brilliant. I used it on my index page below the title which is below the and it does not allow the page to load but keeps my page white and no tool buttons are able to be pressed (to get the source) until the code is entered, then it opens up my index page… HOWEVER if I press the cancel button or the X in the top right hand corner of the password box it bypasses the password and goes straight to the index page that i am trying to protect. any chance of adding something to the code to prevent the cancel and the X button being presses or if it is presses follow the path of an incorrect password???? please. This is the best code i have seen except for this little problem
Thanks