Forum Moderators: mack
I am using a very basic password script (see below) and would like to modify it to set a cookie so that when someone goes to any other pages that have the script, they won't get the password prompt if they have already logged on.
If there's anyone out there who can help, it would be greatly appreciated. Thank you! (I know that security on this script is non-existent, which is fine!)
David
this is the script to prompt the password and lives on the protected page, where it is called later.
function jprot(){
var password = "password" ;
var message = "Please enter your password";
var incmess = "Incorrect password.";
var pw = prompt (message,"");
if (pw!= password) {
alert (incmess);
window.history.back ();
}
}
Would you consider using PHP on these pages, which would mean you could include just one file which handled everything?
You can then use PHP's built in setcookie() [uk.php.net] function.
Should be pretty simple to write and implement, and if you decide to go this way, there are plenty of members in the PHP Forum [webmasterworld.com] to help.
wruk999
I found this at Javascript Page. It had a disclaimer (leave this message here - created by ******) but had to take it out as the link was in there (sorry to who created it - if hes in here). Apparently it stores cookies with a "welcome back" message to the user. Maybe you can compare them?
I'm not much kop with Javascript, still, it may be useful.
<script language="JavaScript"><!--
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 = "real"
if (d == paswd) {
alert("Password confirmed \n \n Welcome back!")
location= "cright.html"
}
if (d == null ¦¦ d!= paswd) {
check_in()
}
function check_in(){
var f = prompt("Enter password to proceed. [pass is 'real']","")
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= "cright.html"
}
else {
alert("You have not entered the password correctly. \n \n However, you can still access the page to find out the script at the next page.")
location = "cwrong.html"
// you could substitute the previous line with "history.back()"
}
}
//-->
</script>
Terry