Beskyt php side med cookie baseret javascript
Jeg har brug for at lave password beskyttelse på en side med et cookiebaseret javascript.Siden er en PHP side, men jeg har kun adgang til header og footer så det er begrænset hvor meget hokus pokus jeg kan lave.
Jeg har fundet dette script, men det "looper" desværre.
Tror det er fordi der på php siden hentes nogle functions
når man navigerer rundt.
(Kan ikke poste URL her, da det er et lukket intranet.)
Kan det tilpasses så det virker?
<script language="JavaScript">
<!--
// please keep these lines on when you copy the source
// made by: Nicolas - http://www.javascript-page.com
function getCookieVal (offset) {
var endstr = document.cookie.indexOf (";", offset);
if (endstr == -1)
endstr = document.cookie.length;
return unescape(document.cookie.substring(offset, endstr));
}
function GetCookie (name) {
var arg = name + "=";
var alen = arg.length;
var clen = document.cookie.length;
var i = 0;
while (i < clen) {
var j = i + alen;
if (document.cookie.substring(i, j) == arg)
return getCookieVal (j);
i = document.cookie.indexOf(" ", i) + 1;
if (i == 0) break;
}
return null;
}
function SetCookie (name, value) {
var argv = SetCookie.arguments;
var argc = SetCookie.arguments.length;
var expires = (argc > 2) ? argv[2] : null;
var path = (argc > 3) ? argv[3] : null;
var domain = (argc > 4) ? argv[4] : null;
var secure = (argc > 5) ? argv[5] : false;
document.cookie = name + "=" + escape (value) +
((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
((path == null) ? "" : ("; path=" + path)) +
((domain == null) ? "" : ("; domain=" + domain)) +
((secure == true) ? "; secure" : "");
}
function DeleteCookie(name) {
var exp = new Date();
FixCookieDate (exp);
exp.setTime (exp.getTime() - 1);
var cval = GetCookie (name);
if (cval != null)
document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}
var d = GetCookie("de")
var paswd = "test"
if (d == paswd) {
alert("Password confirmed \n \n Welcome back!")
location= "index.php"
}
if (d == null || d != paswd) {
check_in()
}
function check_in(){
var f = prompt("Enter password to proceed. [pass is 'test']","")
var thenewdate = new Date ();
thenewdate.setTime(thenewdate.getTime() + (365 * 24 * 3600 * 1000));
SetCookie('de',f,thenewdate);
var e = GetCookie('de');
if (e == paswd) {
alert("Password confirmed \n \n You will not have to enter in the password when you come back")
location= "index.php"
}
else {
alert("You have not entered the password correctly.")
location = "http://www.eksperten.dk"
// you could substitute the previous line with "history.back()"
}
}
//-->
</script>
