javascript 如果未登录,则重定向 html 页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19333043/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 15:12:04  来源:igfitidea点击:

redirect html page if not login

javascripthtmlredirectloginlocation

提问by sam

I have simple Login Form on my webpage. It uses javascript to login user and works fine. problem is that is user types the landing page url directly into the adress bar he directly can acess the page without login. i want to redirect him to the login page if he didn't login. below are the links of loding and target page.

我的网页上有简单的登录表单。它使用 javascript 来登录用户并且工作正常。问题是用户直接在地址栏中输入登陆页面 url,他可以直接访问该页面而无需登录。如果他没有登录,我想将他重定向到登录页面。下面是加载和目标页面的链接。

login: http://samdesign.comli.com/protected/index.html

登录:http: //samdesign.comli.com/protected/index.html

target: http://samdesign.comli.com/protected/user-soft.html

目标:http: //samdesign.comli.com/protected/user-soft.html

and here is the script used are below..

这是使用的脚本如下..

<form>
<table align=center>
  <tr>
     <td>Username:</td>
     <td><input type="text" name="username" style="background:#bfbfbf;color:#212121;border-color:#212121;" onFocus="this.style.background = '#ffffff';" onBlur="this.style.background = '#bfbfbf';"></td>
  </tr>

  <tr>
     <td>Password:</td>
     <td><input type="password" name="password" style="background:#bfbfbf;color:#212121;border-color:#212121;" onFocus="this.style.background = '#ffffff';" onBlur="this.style.background = '#bfbfbf';"></td>
  </tr>

  <tr>
  <td></td>
  <td align=right><input type="button" value="Login" onClick="Login(this.form);" style="background:#bfbfbf;color:#000000;border-color:#212121;" onMouseOver="this.style.color = '#404040';" onMouseOut="this.style.color = '#000000';" onFocusr="this.style.color = '#404040';" onBlur="this.style.color = '#000000';"></td>
  </tr>
</table>
</form>

script is this

脚本是这个

function Login(form) {
username = new Array("numair","sam","u3","u4","u5","u6","u7","u8","u9","u10","temporary");
password = new Array("nk1994","sf1993","p3","p4","p5","p6","p7","p8","p9","p10","user");
page = "/protected/user-soft.html";
if (form.username.value == username[0] && form.password.value == password[0] || form.username.value == username[1] && form.password.value == password[1] || form.username.value == username[2] && form.password.value == password[2] || form.username.value == username[3] && form.password.value == password[3] || form.username.value == username[4] && form.password.value == password[4] || form.username.value == username[5] && form.password.value == password[5] || form.username.value == username[6] && form.password.value == password[6] || form.username.value == username[7] && form.password.value == password[7] || form.username.value == username[8] && form.password.value == password[8] || form.username.value == username[9] && form.password.value == password[9] || form.username.value == username[10] && form.password.value == password[10]) {
self.location.href = page;
}
else {
alert("Either the username or password you entered is incorrect.\nPlease try again.");
form.username.focus();
}
return true;
}

can any one help with exapmle?

任何人都可以帮助示例吗?

回答by

// Put this in your login function, just before the redirect
var sessionTimeout = 1; //hours
var loginDuration = new Date();
loginDuration.setTime(loginDuration.getTime()+(sessionTimeout*60*60*1000));
document.cookie = "CrewCentreSession=Valid; "+loginDuration.toGMTString()+"; path=/";


// Put this at the top of index page
if (document.cookie.indexOf("CrewCentreSession=Valid") == -1) {
  location.href = "/Login.html";
}

回答by TKumar Stpl

this is not the right way doing authentication, you must keep authentication logic at server-end, you can use Node.js/Ruby-on-Rails(ROR)/JSP/ASP.NET(etc what ever you want) at your server-end.

这不是进行身份验证的正确方法,您必须在服务器端保留身份验证逻辑,您可以在服务器端使用Node.js/Ruby-on-Rails(ROR)/JSP/ASP.NET(等等)。

and for making your page secure you have to add before filter at server-end (that will check user's session).

为了使您的页面安全,您必须在服务器端过滤器之前添加(这将检查用户的会话)。

回答by Gavin

JavaScript authentication?? This isn't secure. All authentication should be done server side.

JavaScript 身份验证?这不安全。所有身份验证都应在服务器端完成。