javascript 密码保护页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18095144/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Password Protect Page
提问by LouTrev
I took the following code from another webpage "http://www.javascriptkit.com/script/cut10.shtml".
我从另一个网页“ http://www.javascriptkit.com/script/cut10.shtml”中获取了以下代码。
<SCRIPT>
function passWord() {
var testV = 1;
var pass1 = prompt('Please Enter Your Password',' ');
while (testV < 3) {
if (!pass1) history.go(-1);
if (pass1.toLowerCase() == "letmein") {
alert('You Got it Right!');
window.open('protectpage.html');
break;
}
testV+=1;
var pass1 = prompt('Access Denied - Password Incorrect, Please Try Again.','Password');
}
if (pass1.toLowerCase()!="password" & testV ==3) history.go(-1);
return " ";
}
</SCRIPT>
<CENTER>
<FORM>
<input type="button" value="Enter Protected Area" onClick="passWord()">
</FORM>
</CENTER>
It works only in case the password is entered second time, but not when it is entered first time. When you enter password it says wrong password prompting you to enter the password again and then it goes through.I need a script that shall prompt me of the correct password, in case I enter the wrong password. Can any one help me with the code as i am a beginner in JavaScript.
它仅在第二次输入密码时有效,但在第一次输入时无效。当您输入密码时,它说错误的密码提示您再次输入密码,然后它通过。我需要一个脚本来提示我正确的密码,以防我输入错误的密码。任何人都可以帮助我编写代码,因为我是 JavaScript 的初学者。
回答by Luke
Your code appears to workwhen visiting the link.
访问链接时,您的代码似乎有效。
I know you're learning. Still, you shouldn't be doing authentication like this though as you're not really protecting anything. Anyone can read the source code by using the "View Page Code" option in any browser (typically right click on the page). This means anyone can easily get your password.
我知道你在学习。不过,您不应该像这样进行身份验证,因为您并没有真正保护任何东西。任何人都可以通过使用任何浏览器中的“查看页面代码”选项(通常在页面上单击鼠标右键)来阅读源代码。这意味着任何人都可以轻松获取您的密码。
For true authentication you should be using either a server side language (like PHP), or HTTP Digest authenticationconfigured by your web server. Digest is a bit out of date as it uses MD5, but it's a million times better than what you're doing.
对于真正的身份验证,您应该使用服务器端语言(如 PHP)或由您的 Web 服务器配置的HTTP Digest 身份验证。Digest 有点过时,因为它使用 MD5,但它比您正在做的要好一百万倍。
For more information about setting up HTTP Digest with Apache web server see:
有关使用 Apache Web 服务器设置 HTTP 摘要的更多信息,请参阅:
http://httpd.apache.org/docs/2.2/mod/mod_auth_digest.html
http://httpd.apache.org/docs/2.2/mod/mod_auth_digest.html
For doing the same with Nginx:
用 Nginx 做同样的事情:
http://wiki.nginx.org/HttpAuthDigestModule
http://wiki.nginx.org/HttpAuthDigestModule
The HTTP Basic authenticationworks too, but it transmits the password from the user's browser in plain text. With HTTP digest the password is hashed.
该HTTP基本身份验证工作过,但它传输从用户的纯文本浏览器的密码。使用 HTTP 摘要,密码被散列。
Knowing that you're learning JavaScript, your best bet is to configure the web server you're using. Since most web hosting services use Apache, you can most likely use an .htaccess file. You can search ".htaccess http digest"for tutorials on how to set this up.
知道您正在学习 JavaScript,最好的办法是配置您正在使用的 Web 服务器。由于大多数网络托管服务使用 Apache,您很可能可以使用.htaccess 文件。您可以搜索“.htaccess http 摘要”以获取有关如何设置的教程。
Some web hosting services have control panels that have a feature to protect directories using Digest/Basic auth. In cPanel, which is quite common, it's called "Password Protect Directories".
一些网络托管服务的控制面板具有使用摘要/基本身份验证保护目录的功能。在 cPanel 中,这很常见,它被称为“密码保护目录”。
If you were more advanced I would suggest doing it in PHP, but thats a rather complicated subject.
如果你更高级,我会建议用 PHP 来做,但那是一个相当复杂的主题。
回答by Sameer IRH
//Add this to you script
function run(){
var password = prompt("Password Please");
//Change to your own Password
if(password != 'Sameer'){
document.body.innerHTML = '';
document.body.innerHTML = 'Password Failed! Reload to Renter Password';
}else{
alert('Success');
}
}
run();
回答by Guruprasad Rao
Try this.. Just add access denied prompt inside the false case...
试试这个..只需在错误的情况下添加拒绝访问提示......
function passWord()
{
var testV = 1;
var pass1 = prompt('Please Enter Your Password',' ');
while (testV < 3)
{
if (!pass1)
{
history.go(-1);
var pass1 = prompt('Access Denied - Password Incorrect, Please Try
Again.','Password');
}
else if (pass1.toLowerCase() == "letmein")
{
alert('You Got it Right!');
window.open('protectpage.html');
break;
}
testV+=1;
}
if (pass1.toLowerCase()!="password" & testV ==3)
history.go(-1);
return " ";
}
回答by Marko Makismovic
I used the same exact code that you have and the problem is the the first time for don't know what reason it added one space in front of the input. Just make sure that you don't have that and it will be fine.
我使用了与您拥有的完全相同的代码,问题是第一次因为不知道是什么原因在输入前添加了一个空格。只要确保你没有那个就可以了。