HTML登录页在页面源中显示凭据

时间:2019-03-15 13:34:55

标签: javascript html forms

好的,所以我试图为一组我的朋友创建一个私人网站,让其他人无法注册或登录。我的脚本是...

<html>
<head>
<title>
Login page
</title>
</head>
<body>
<h1 style="font-family:Comic Sans Ms;text-align="center";font-size:20pt;
color:#00FF00;>
Simple Login Page
</h1>
<form name="login">
Username<input type="text" name="userid"/>
Password<input type="password" name="pswrd"/>
<input type="button" onclick="check(this.form)" value="Login"/>
<input type="reset" value="Cancel"/>
</form>
<script language="javascript">
function check(form)/*function to check userid & password*/
{
 /*the following code checkes whether the entered userid and password are matching*/
 if(form.userid.value == "myuserid" && form.pswrd.value == "mypswrd")
  {
    window.open('target.html')/*opens the target page while Id & password matches*/
  }
 else
 {
   alert("Error Password or Username")/*displays error message*/
  }
}
</script>
</body>
</html>

使用该代码,某些人可以打开页面源代码,并查看凭据。有什么方法可以隐藏和加密它,甚至可以添加一些其他帐户?

3 个答案:

答案 0 :(得分:1)

您永远不要在客户端代码中放置敏感信息,因为查看页面源或检查元素的任何人都可以看到敏感信息。您应该在客户端验证的唯一内容是必填字段或有效的电子邮件等内容。然后,在服务器端脚本中执行敏感验证,就像上面所做的一样。我还必须指出,使用if语句检查用户是非常糟糕的做法。您应该更多地处理应用程序的服务器端逻辑。

答案 1 :(得分:1)

您用客户端JavaScript编写的任何内容都有可能被其他人在浏览器中看到,因此,身份验证之类的事情应在服务器上处理。

知道不要使用明文形式存储密码也很重要。

通过阅读您的代码,我可以看到的另一件重要的事情是,我可能只是导航到https://whatever.com/target.html并看到您要使用密码保护的页面。

如果您有兴趣使用这种功能来构建网站,则需要使用服务器端语言并为服务器编写一些代码。

答案 2 :(得分:0)

执行服务器端身份验证的最简单方法是使用.htaccess和.htpasswd文件进行基本的HTTP身份验证。