【问题标题】:user see html page when we enter password当我们输入密码时,用户会看到 html 页面
【发布时间】:2020-02-21 05:44:02
【问题描述】:

我在我的 index.html 中为网页添加了这个脚本以保护密码,但它不能在同一页面工作

<script type="text/javascript">
	var s ="";

	while (s!= "12345678")
	{
	s=prompt("please enter your password");
	if (s=="12345678")
	{
	window.location.href="http://localhost/know21feb"; //page to redirect if password entered is correct

	}
	else
	{
	alert("Incorrect password-Try again");

	}
	}
	</script>

【问题讨论】:

  • 这远非一种安全的密码验证方式。用户可以检查网页并获取密码,或者只是从原始 HTML 访问链接。用于安全验证用户的可用机制会因您向最终用户提供网页的方式而异。
  • 使用服务器端编程语言(例如 PHP)会更安全,然后使用 XML HTTP 请求与 PHP 文件进行通信。
  • 您可以在您的网络服务器上设置 HTTP 授权标头。这可能是最简单但安全的解决方案

标签: javascript html password-protection


【解决方案1】:

如果您正在寻找一个安全的密码系统,这不会成功,因为密码位于源代码中,任何人都可以查看他们是否查看页面源代码或检查它。无论如何,我会这样做:

var s;
var password = "12345678";

while(true){
    if(s != password){
        s = prompt("Please enter password!");
    } else {
        break;
    }
}

// Code following this will execute if the password was correctly inputted

Here's a fiddle

【讨论】:

    【解决方案2】:

    如果您只想验证客户端验证以用于学习目的,您可以使用HTML5 localstorage like

      window.localStorage.setItem("password", "123456"); // set default password in key named password
        if (window.localStorage.getItem("password") == document.getElementById("password").value) {
             // you logic
          } else {
               alert("Incorrect password-Try again");
          }
    

    你也可以使用你的函数,你可以将密码作为参数传递并检查条件

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-13
      • 1970-01-01
      • 1970-01-01
      • 2013-11-07
      • 2011-09-26
      • 1970-01-01
      • 2012-09-10
      • 1970-01-01
      相关资源
      最近更新 更多