【发布时间】:2018-07-30 02:59:45
【问题描述】:
我正在尝试使用密码保护我的网页(例如http://mywebsite.com/),以便用户每次会话只需输入一次密码。这是我的问题:如果用户取消了初始提示或输入了错误的密码,然后被重定向到 google.com,然后重新访问 http://mywebsite.com/ 它允许他们在不提示输入密码的情况下查看页面。
不知道我做错了什么来解决这个小问题的后门。
这是我尝试实现的 JavaScript:
//Protect only once per browser session? (0=no, 1=yes)
//Specifying 0 will cause protect to load every time page is loaded
var once_per_session=1
function get_cookie(Name) {
var search = Name + "="
var returnvalue = "";
if (document.cookie.length > 0) {
offset = document.cookie.indexOf(search)
if (offset != -1) { // if cookie exists
offset += search.length
// set index of beginning of value
end = document.cookie.indexOf(";", offset);
// set index of end of cookie value
if (end == -1)
end = document.cookie.length;
returnvalue=unescape(document.cookie.substring(offset, end))
}
}
return returnvalue;
}
function passwordProtect(){
var password;
var pass1 = "thePassword";
password = prompt('Enter password to view page',' ');
if(password == pass1){
alert('Correct password, click ok to enter');
window.location="http://mywebsite.com";
}
else {
window.location="http://google.com";
}
}
function loadornot(){
if (get_cookie('protect')==''){
passwordProtect()
document.cookie="protect=yes"
}
}
if (once_per_session==0)
passwordProtect()
else
loadornot()
【问题讨论】:
-
理想情况下,仅在使用后端验证后才提供敏感数据 - 对前端的更改不会可靠安全。毕竟,任何人都可以看到 Javascript 源代码。
-
@CertainPerformance 我将如何处理?我会用 php 代替吗?
-
我认为这只是为了“学习”目的?
-
是的,是@Ralph
-
那么给出的答案是对的,loadornot() 不会检查 passwordProtect 的结果。它将始终调用设置 cookie 的行。您可能假设如果您将 window.location 设置为一个新位置,它之后的脚本将停止,这是不正确的。从您的密码保护中返回一个真/假,并使用返回值来决定您是否将添加保护='yes' cookie。
标签: javascript html webpage password-protection