I'm trying to password protect my webpage (http://mywebsite.com/ for example) so that the user only has to enter the password once per session. Here's my problem: If the user cancels out of the initial prompt or enters a wrong password and then get redirected to google.com, AND THEN revisit http://mywebsite.com/ it allows them to view the page without prompting for a password.
Not sure what I'm doing wrong to resolve this small back door of a problem.
Here's the JavaScript I'm attempting to implement:
//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()