javascript 非敏感链接的简单密码保护
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11955828/
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
Simple password protection for non-sensitive links
提问by Jeff
I've been looking at password protection for HTML and most of the protection listed appears to be overkill for my needs.
我一直在研究 HTML 的密码保护,列出的大多数保护似乎对我的需求来说太过分了。
I just want to have some portfolio images (nothing that needs to be private and it doesn't matter if any advanced programmer or someone that knows how to open the code can get into it because nothing is sensitive), I just don't want the average user to access certain areas of site and only allow someone who I give the password (for example on my business card.)
我只想有一些投资组合图片(没有什么需要私密的,如果任何高级程序员或知道如何打开代码的人都可以进入它并不重要,因为没有什么是敏感的),我只是不想普通用户访问网站的某些区域,只允许我提供密码的人(例如在我的名片上)。
Does anyone have any suggestions or direction I should look?
有没有人有我应该看的任何建议或方向?
回答by Frank Orellana
Use javascript in your main page, something like this:
在主页中使用 javascript,如下所示:
<body>
Enter password: <input id='password' type='text' />
<a href="your_image_portfolio.html" onclick="javascript:return validatePass()">enter your password and click this</a>
<script>
function validatePass(){
if(document.getElementById('password').value == 'yourBussinessCardPassword'){
return true;
}else{
alert('wrong password!!');
return false;
}
}
</script>
</body>
It's very insecure, but it will do what you want for non technical users...
这是非常不安全的,但它会为非技术用户做你想做的事......
回答by Wegko
As said above, HTML doesn't support password protection directly. You can do this with PHP: create a form to enter a password that sends data to a PHP script. Use an if statement at the beginning of the PHP script that checs whether the submitted data matches your password. If it doesn't, redirect/include some sort of error page. If it does, have it load the rest of the gallery from inside the if statement.
如上所述,HTML 不直接支持密码保护。您可以使用 PHP 执行此操作:创建一个表单以输入将数据发送到 PHP 脚本的密码。在 PHP 脚本的开头使用 if 语句来检查提交的数据是否与您的密码匹配。如果没有,重定向/包含某种错误页面。如果是,让它从 if 语句中加载库的其余部分。
<?php
if ($_POST["password"] == 12345) {
page goes here
}
else {
include 'errorpage'
?>
回答by japha
<script type="text/JavaScript">
function Sinfo()
{
var info = document.getElementById("info").value; //Getting the value in the Text Area
var help = "Hello"; // setting the value for the password
if(info==help){ // checking to see if info is equal to help
document.write("Correct Password"); // writing correct password if correct
}
else{
document.write("Wrong Password"); // writing the wrong password if wrong
}
}
</script>