密码保护 html 页面

发布于 2024-11-18 08:43:54 字数 484 浏览 5 评论 0原文

我需要对任意 html 文件添加一些简单的密码保护。它确实需要安全,但只需将随机的无赖拒之门外即可。我尝试使用下面的JS:

var password; 
    var thePassword="secretpassword"; 
    password=prompt('Enter Password',' '); 
    if (password==thePassword) { alert('Correct Password! Click OK to Enter!'); }
    else { window.location="http://www.google.com/"; } 

这在Firefox中工作正常,但似乎提示功能在IE中失败,所以我们总是重定向到google...

关于如何使用直接HTML页面进行简单的密码保护有什么建议吗?

编辑:要明确的是,它在 Firefox 中工作正常,在 IE 中甚至不会弹出要求“输入密码”的提示

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 Password! 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"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一笑百媚生 2024-11-25 08:43:54

在 IE 中对我来说工作得很好。演示:http://jsfiddle.net/U2n3P/

它可能无法正常工作的一个可能原因是您'使用 ' ' 重新填充提示符中的空格,因此当您开始键入时,末尾可能会有空格。将提示更改为:

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

仅供参考,我知道您说过您不需要超级安全,但您不妨添加一些安全性。获取 MD5 库并执行以下操作:

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

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){
仙气飘飘 2024-11-25 08:43:54

将安全页面或目录命名为 md5 哈希密码。

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"); }

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"); }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文