Click to See Complete Forum and Search --> : [RESOLVED] Password code not working


Nobby
04-11-2006, 02:53 AM
I have a password page where clients can enter a predetermined password. I have it set up so that the ID and Password are stored on a page called pwd.js and when the client enters his password a function, pasuser(form), is called from pwd.js. I'm new to javascript and what I can't work out is how to use this for more than one password. Can I use "else if" and enter different passwords and if so where do I put the code? Can anybody help please?
Here is the code I have:

password code
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<script language="javascript" type="text/javascript" src="pwd.js"></script>
</head>

<body>
<table width="780" border="0" cellspacing="2" cellpadding="2" >
<form name="login">
<tr>
<td><font color=#999999 size="2" face="arial">User Name:</font></td>
<td><input name="id" type="text" value size="36"></td>
</tr>
<tr>
<td><font color=#999999 size="2" face="arial">Password:</font></td>
<td><input name="pass" type="password" value size="36"></td>
</tr>
<tr>
<td><input type="button" value="Login" onClick="pasuser(this.form)">
<input type="Reset">
</td>
</tr>
</form>
</table>
</body>
</html>

pwd.js code

<!--//
function pasuser(form) {
if (form.id.value=="client1") {
if (form.pass.value=="password") {
location="client1/page1.html"
} else {
alert("Invalid Password")
}
} else { alert("Invalid UserID")

}
}
//-->

Any help would be greatly appreciated

hyperlisk
04-11-2006, 11:33 PM
Try this for your script:

users = new Array();
users["name1"] = "password1";
users["name2"] = "password2";
users["name3"] = "password3";
// Add more as necessary...

function pasuser(form) {
username = form.id.value;
password = form.pass.value;
if(users[username] && users[username] == password){
// This will take each user to their directory and the "page1.html" in their directory.
window.location.href = username +"/page1.html";
} else {
alert("You have put in an incorrect username and/or password.\nPlease make sure the information is correct.");
}
}

felgall
04-12-2006, 01:26 AM
What's the point in more than one password in Javascript. Anyone wanting to access the page will just view the source to see what the password is anyway so allowing more than one just gives people a choice of which one they use.

Of course with that script most people wont bother with a password and will just go straight to the destination page.

Nobby
04-12-2006, 05:35 AM
Thanks hyperlisk, works a treat!

Felgall - the script is on a separate .js page and the people accessing the site will not have any idea how to access source code. The information that is accessed is not secure anyway. If you have nothing positive to say, dont waste space with pointless replies. Not everyone is as computer savvy as you.

hyperlisk
04-12-2006, 11:15 AM
But if your site grows big, there are bound to be people that will view your code and mess with other peoples stuff. That is a very insecure way to do user logins. If you have access to a server-side language, than you should look into using a database.

felgall
04-12-2006, 03:51 PM
My reply wasn't pointless. There are lots of people who will read this thread who would not realise how pointless that it is to try to do password protection using Javascript if it wasn't pointed out to them. You may decide that it doesn't matter that anyone past the novice stage can bypass your script but someone else reading this thread may have been thinking of using Javascript to protect a page where real security is needed and then they would end up in big trouble when it didn't work.

When you ask a question on a public forum then other people with the same question will search the forum and find where their question has already been answered rather than clogging up the forum by asking the same question over again (at least that is what they are supposed to do and what most will do). Any answer that relates to a specific situation therefore needs to make it clear what the special circumstances are so as to not mislead subsequent readers.

Nobby
04-13-2006, 01:54 AM
Sorry guys if I've caused offence, I'm new to this. The site is a small site for photographers in the southeast of England within a 50 mile radius, its not advertised, its not commercial and can only be accessed if you know the URL. There is a need for people to be able to access their own directory, and Hyperlisks code allows me to do this. The password is superficial. There will never be more than 20 or so users and I dont expect hackers to be interested in such a small thing. Or would they?
Once again, thanks for your help.