...

View Full Version : Password Script



Ben@WEBProp
10-09-2003, 09:46 PM
I am using a javascript password system to keep out Average Joe from a certain page. This is the script:

<script type="text/javascript"><!--

var n=3;

var pass=prompt("What is the password, you have "+n+" chance(s) left.","");n--;

while(pass.toLowerCase()!="password" && n>0){pass=prompt("What is the password, you have "+n+" chance(s) left.","");n--}

if(pass.toLowerCase()!="password"){location.replace("http://www.w3.org/");}

//--></script>

If this script is placed on a page, a dialoge box pops up and prompts the user for a password in order to enter the page. This box comes with an "OK" and a "Cancel" button. The thing works great, except it just lets you in if you push the cancel button. Does anyone know how I can get the cancel button to redirect the user to a different page or any other way to fix this? Thanks.

-Ben

Danne
10-09-2003, 10:01 PM
Put this after the prompt:


if (!pass && typeof pass=="object") {
alert("you pressed cancel");
}

Ben@WEBProp
10-09-2003, 10:18 PM
Well, its an improvement... but the only difference is know that it alerts the user that he/she has clicked cancel, and then lets them into the page anywho. Is there something to add to that so that if the cancel button is clicked, it redirects them to another site?

Thanks!

-Ben

Danne
10-09-2003, 10:25 PM
Yes:



if (!pass && typeof pass=="object") {
pass = "wrongPassword";
}


Or:


if (!pass && typeof pass=="object") {
location = "anotherpage.html";
}


:)

Ben@WEBProp
10-09-2003, 10:42 PM
That worked great! I had to take out the multiple-tries part to get it to work correctly, but it works great! Thanks!

-Ben

fredmv
10-09-2003, 10:54 PM
There was just a huge thread on password protection using JavaScript a few days ago. You might want to consider checking it out:

http://www.codingforums.com/showthread.php?s=&threadid=25686

Danne
10-09-2003, 11:52 PM
Originally posted by Ben@WEBProp
That worked great! I had to take out the multiple-tries part to get it to work correctly, but it works great! Thanks!

-Ben

No problem if you want to use multiple tries:


var n=3;
var pass="wrongPassword";
while(pass.toLowerCase()!="password" && n>0){
pass=prompt("What is the password, you have "+n+" chance(s) left.","");
n--;
if (!pass && typeof pass=="object") {
pass="wrongPassword";
}
}

if(pass.toLowerCase()!="password"){
location.replace("http://www.w3.org/");
}

Ben@WEBProp
10-10-2003, 12:29 AM
Oh, ya, the thing still WORKS, but it is less secure. If you make a wrong guess or click ok first, and then on the second, third, etc try you push cancel, it lets you in. However, the person should either know the password, or not. So I think I will just stick with the one try thing.

Thanks again, you were a great help!

-Ben

SDP2006
10-10-2003, 12:58 AM
PHP .htaccess works great, but I'm not going there since this is the javascript forum...



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum