带有验证的self.location.href

时间:2013-01-22 22:28:45

标签: javascript

我真的可以为我的网站提供表单重定向和验证问题的帮助。

我已成功获取输入“密码”以重定向到输入值,但是如果用户输入密码错误,则会出现404错误 - 这是正确的,但对用户来说并不是很好。

我的问题是,无论如何都要在服务器上对txt或xml文件进行验证,并在提交之前使用密码列表进行验证?因此,如果错误而不是404,则用户会收到警报!

以下是目前有效的示例,感谢您的帮助!!

<html><head>
<SCRIPT TYPE="text/javascript">

function login() {
   if (validLogin()) {
      password = document.userInfos.password.value;
      self.location.href=
        ""+password+"";
      }
   }
function validLogin() {
   if (isBlank(document.userInfos.password.value)){
      alert("Can't be blank");
      document.userInfos.password.focus();
      return false;
      }
   return true;      
   }
function isBlank(s) {
   return (s == "");
   }

   function doKey(e) {
//check to see if the return key was pressed ...
if( event.keyCode == 13){ 
//call the login function ...
login();
//prevent the page from reloading ...
return false;
}
}

//listen for keypress events and then call doKey()
document.onkeypress = doKey;

</SCRIPT>

</head>

        <form  NAME="userInfos" >


<INPUT TYPE="password" NAME="password" LENGTH="20" style="color: #333333" size="23" >&nbsp;&nbsp;&nbsp;&nbsp;<INPUT TYPE="button" style="color: #333333" onClick="login()" VALUE=" View ">
</form>

    </body>
</html>

1 个答案:

答案 0 :(得分:1)

不要在客户端JavaScript中检查密码;任何访问者都可以使用“查看源”获取可用的密码。如果可能,请使用PHP脚本,CGI脚本或htpasswd文件检查服务器上的密码。

如果您使用的免费网络主机不允许更好的选项,您可以为您的网址发出AJAX请求并检查状态代码:

var prefix = "/~myusername/passwords/", suffix = ".html";
var url = prefix + document.userInfos.password.value + suffix;

var xhr = new XMLHttpRequest(); // need some more code to support IE 6
xhr.open('GET', url);

xhr.onreadystatechange = function() {

    if (xhr.readyState !== 4) {
        return;
    }

    if (xhr.status === 200) {
        // It's possible to grab user-specific settings from xhr.responseText,
        // (e.g. the URL to redirect to), but for simplicity's sake...
        location.href = url;
    } else {
        alert('Password incorrect');
    }

};

xhr.send();

但请注意以下限制

  • 受保护的HTML文件应位于登录页面和密码文件的单独目录中。

  • 该目录的名称必须难以预测

  • 撤消用户的访问权限,您需要重命名包含受保护HTML文件的目录,并相应地更新所有其他用户密码的文件。< / p>

  • 直接链接到受保护的文件会暴露它们(直到目录被重命名),受保护的文件可能会出现在Google 或其他搜索引擎上,原因有各种已知和未知。< / p>

  • 没有针对暴力密码猜测通过日志文件曝光密码的保护。