PDA

View Full Version : JavaScript PasswordProtection Gate


Stroup
06-06-2002, 10:27 AM
}:-)
I am new to JavaScripting and this is my very first script. Well you see I play this online game, and I have a site that I would like to have a members area in. This passwordCode i have been having troubles with. Can someone point me in the right direction?

<HTML>
<SCRIPT LANGUAGE="JScript">
<BODY onload=getpassword()>
function getpassword()
var password = prompt
("Im gonna need to see some ID"), ("xxxxxxxx")
if (password == "assassin") {
document.writln("<BODY><H1>Good your one of us!</H1>
Sorry about that but the boss says we need it.
</BODY>")
else
document.writeln("<BODY><H1>Incorrect</H1>You have attempted to access
our site without registered password. Please leave or
hit your <B>back</B> button and try again. </BODY>")

</SCRIPT>
</HEAD>
</BODY>
</HTML>

cpradio
06-06-2002, 11:24 AM
first off javascript is not the best way to make a password protection as all a person would have to do is view the page source for the password

But if you want to do this you would use the following code:

<html><head><title></title>
<script language="JavaScript" type="text/javascript">
function checkPassword() {
if(document.form1.password1.value == "xxxxxx")
window.location = "members/index.html";
else
window.location = "error.html";
return false;
}
</script>

<body>
<form name="form1" method="post" onSubmit="checkPassword()">
<input type="password" name="password1"><br>
<input type="submit" value="Submit">
</form>
</body>
</html>

Stroup
06-06-2002, 12:02 PM
Thank-you for your reply. I still can't get even that code to work though... :(


Is there another way I could make a password protection gate that would not be easy to get into?

cpradio
06-06-2002, 12:08 PM
You need to modify xxxxxx to whatever you want the password to be.

Other options are .htaccess, php, perl, cgi, asp, jsp, etc. (Assuming you are on an actual server and not a free one)

Stroup
06-06-2002, 02:48 PM
I have been playing around with the code for a few hours now.. It doesn't seem to work. *sigh* What can I say I'm a :newbie:

cpradio
06-06-2002, 02:57 PM
<html><head><title></title>
<script language="JavaScript" type="text/javascript">
function checkPassword() {
alert(document.form1.password1.value);
if(document.form1.password1.value == "html")
window.location.href = "http://cpradio.net";
else
window.location.href = "http://htmlforums.com";
}
</script></head>

<body>
<form name="form1" method="post">
<input type="password" name="password1">
<input type="button" value="Submit" onClick="checkPassword()">
</form>
</body>
</html>

Change the two websites to the locations you want them to go if the password is correct or incorrect.

First website is the correct location (aka, members pages)
Second should lead to an error page.