开发者

Password protect html page

开发者 https://www.devze.com 2023-03-16 19:46 出处:网络
I need to tack some simple password protection onto arbitrary html files.It does need need to be secure, but just keep out the random roff-raff.I tried using the below JS:

I need to tack some simple password protection onto arbitrary html files. It does need need to be secure, but just keep out the random roff-raff. I tried using the below JS:

var password; 
    var thePassword="secretpassword"; 
    password=prompt('Enter Password',' '); 
    if (password==thePassword) { alert('Correct P开发者_如何学Cassword! Click OK to Enter!'); }
    else { window.location="http://www.google.com/"; } 

This works fine in Firefox, but seems that the prompt function fails in IE and so we always redirect to google...

Any suggestions on how to do a simple password protection using straight HTML pages?

EDIT: to be clear, it works fine in Firefox, and in IE does not even prompt with a popup asking to "Enter Password"


Works fine for me in IE. Demo: http://jsfiddle.net/U2n3P/

One possible reason it may not be working is that you're populating the prompt with an empty space, by using ' ', so when you start typing there may be a space at the end. Change the prompt to:

password=prompt('Enter Password', ''); 

FYI, I know you said you didn't need this to be super secure, but you might as well add some security. Get an MD5 library and do, instead:

var thePassword = "md5encodedpassword";
password=prompt('Enter Password',' ');
if(md5(password) != thePassword){


Name the secure page or directory the md5 hashed password.

function isThere(url) {
    var req= new AJ(); // XMLHttpRequest object
    try {
        req.open("HEAD", url, false);
        req.send(null);     
        return req.status== 200 ? true : false;
    }
    catch (er) {
        return false;
    }
}
password=prompt('Enter Password',' '); 
password=md5(password);
if (isThere(md5 + "/") { window.location = password + "/"; }
else { alert("incorrect"); }
0

精彩评论

暂无评论...
验证码 换一张
取 消