简体   繁体   English

登录尝试和重定向

[英]Login attempts and redirect

I have created some code for a login page. 我已经为登录页面创建了一些代码。 What I want is, if the username and password are correct, then go to the Home.html page. 我想要的是,如果用户名和密码正确,那么请转到Home.html页面。 Otherwise, stay on the login page but only give the user 3 attempts to log in. I can't seem to figure out what is wrong as it doesn't count how many attempts I make nor does it redirect to the Home.html page when I get it right. 否则,请保持登录页面,但只会让用户3尝试登录。我似乎无法弄清楚出了什么问题,因为它不计算我做了多少次尝试,也没有重定向到Home.html页面当我做对了。

 var counter = 0; function checkdetails() { var name = "", password = ""; name = form1.txtusername.value password = form1.txtpassword.value if (name == "Shauna" && password == "8nss") { window.alert("Both right") window.location.replace("Home.html"); form1.txtusername.value = "" form1.txtpassword.value = "" } else if (name == "Shauna" && password != "8nss") { window.alert("Incorrect Password.") counter = counter + 1 window.alert(3 - counter + " attempts left") form1.txtusername.value = "" form1.txtpassword.value = "" } else if (name != "Shauna" && password == "8nss") { window.alert("Incorrect Username") counter = counter + 1 window.alert(3 - counter + " attempts left") form1.txtusername.value = "" form1.txtpassword.value = "" } else if (name != "Shauna" && password != "8nss") { window.alert("Both Wrong") counter = counter + 1 window.alert(3 - counter + " attempts left") form1.txtusername.value = "" form1.txtpassword.value = "" } if (counter == 3) { window.alert("Incorrect details - Login failed") window.close() } } 
 <form action="" method="post" name="form1" onsubmit="checkdetails()"> <table bgcolor="white" width="500" border="0" align="center"> <col width="200"> <col width="200"> <tr> <th colspan="2" align="center" bgcolor="grey">Login</th> </tr> <tr> <td>&nbsp;</td> </tr> <tr> <td colspan="2" align="center">Username</td> </tr> <tr> <td colspan="2" align="center"> <label> <input type="text" name="txtusername" id="txtusername" class="info" required/> </label> </td> <tr> <td>&nbsp;</td> </tr> <tr> <td colspan="2" align="center">Password</td> </tr> <tr> <td colspan="2" align="center"> <label> <input type="password" name="txtpassword" id="txtpassword" class="info" required/> </label> </td> </tr> <tr> <td>&nbsp;</td> </tr> <tr> <td colspan="2"> <label> <input type="checkbox" name="terms" value="terms" id="terms" />Remember Me</label> </td> </tr> <br> <br> <br> <tr> </tr> <tr> <td>&nbsp;</td> </tr> <tr> <td> <label> <input type="submit" name="Login" id="Login" value="Login" /> </label> </td> <td> <label> <input type="reset" name="Reset" id="Reset" value="Reset" /> </label> </td> </tr> </table> </form> 

Here's one way of doing what you're trying to do via JavaScript. 这是通过JavaScript做你想做的事情的一种方式。 I'm handling the onsubmit of the form and using JavaScript to validate it and return true or false to the handler, depending on how well it validated. 我正在处理表单的onsubmit并使用JavaScript来验证它并返回true或false给处理程序,具体取决于它的验证程度。 I removed quite a bit of the duplication of code effort too and simplified the logic. 我也删除了相当多的代码工作重复并简化了逻辑。

NB this is a very poor way of handling logins and client side validation of login attempts, but given that it's just for an assignment exercise, this is just one way to do what you need. 注意,这是处理登录尝试的登录和客户端验证的一种非常糟糕的方式,但鉴于它仅用于分配练习,这只是一种方法来完成您的需要。

<meta charset="UTF-8">
<script>
var remainingAttempts = 3;

function checkDetails() {
    var name = form1.txtusername.value;
    var password = form1.txtpassword.value;
    console.log('name', name);
    console.log('password', password);
    var validUsername = validateUsername(name);
    var validPassword = validatePassword(password);
    if (validUsername && validPassword) {
        alert('Login successful');
    } else {
        form1.txtusername.value = '';
        form1.txtpassword.value = '';
        remainingAttempts--;

        var msg = '';
        if (validPassword) {
            msg += 'Username incorrect: ';
        } else if (validUsername) {
            msg += 'Password incorrect: ';
        } else {
            msg += 'Both username and password are incorrect: ';
        }

        msg += remainingAttempts + ' attempts left.';
        alert(msg);

        if (remainingAttempts <= 0) {
            alert('Closing window...');
            window.close();
        }
    }

    return validUsername && validPassword;
}

function validateUsername(username) {
    return username == 'Shauna';
}

function validatePassword(password) {
    return password == '8nss';
}
</script>

<form id="form1" name="form1" method="post" action="Home.html" onsubmit="return checkDetails();">
<table>
<tr>
<th colspan="2" align="center" bgcolor="grey">Login</th></tr>
Username
<tr><td colspan="2" align="center"><label><input type="text" name="txtusername" id="txtusername" class="info" required /></tr>
<tr><td>&nbsp;</td></tr>
<tr><td colspan="2" align="center">Password</td></tr>
<tr>
    <td colspan="2" align="center"><label>
    <input type="password" name="txtpassword" id="txtpassword" class="info" required/>
  </label></td>
</tr>
<tr>
    <td colspan="2">
        <label>
        <input type="checkbox" name="terms" value="terms" id="terms"/>Remember Me</label>
    </td>
</tr>
<br><br><br>
<tr>
</tr>
<tr>
    <td>
        <label>
        <input type="submit" name="Login" id="Login" value="Login"/>
        </label>
    </td>
    <td>
        <label>
        <input type="reset" name="Reset" id="Reset" value="Reset" />
        </label>
    </td>
</tr>
</table>
</form>

If you want to redirect user in JavaScript you should do 如果您想在JavaScript中重定向用户,您应该这样做

function Redirect() {
    window.location="http://target.url";
}

Assimilated this, is foolish manage login in JavaScript so I think you should read something that explain differences between client-side and server-side executed code. 同化这一点,是愚蠢的管理登录JavaScript,所以我认为你应该阅读一些解释客户端和服务器端执行代码之间的差异的东西。

EDIT. 编辑。 For managing the max three-time login you can store data in Local Storage 要管理最多三次登录,您可以将数据存储在本地存储中

Enforcing a limit on login attempts on the client end is a serious security problem . 在客户端上强制执行登录尝试是一个严重的安全问题 I wrote scrapers that reverse-engineered what the JavaScript on a page did and submitted valid looking requests. 我编写了一些反汇编程序,用于反向设计页面上的JavaScript并提交有效查看请求。 A programmer could easily write a script that uses curl to keep trying username/password combinations until they succeed without worrying about your JavaScript. 程序员可以轻松编写一个使用curl的脚本来继续尝试用户名/密码组合,直到成功为止,而不用担心你的JavaScript。 You should make sure that the server blocks excessive login attempts before you worry about how the client end works. 在担心客户端的工作方式之前,应确保服务器阻止过多的登录尝试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM