Click to See Complete Forum and Search --> : Login script for multiple usernames


annasue
02-27-2003, 05:30 PM
I downloaded the attached script. It works as long as the username is "guest" and the password is "login". I can't determine from looking at the code where/how you add/delete username/password combinations that are valid???

---------------------------


<!-- TWO STEPS TO INSTALL MULTIPLE USERS:

1. Copy the first code into the HEAD of your HTML document
2. Put the last coding into the BODY of your HTML document -->

<!-- STEP ONE: Copy this code into the HEAD of your login HTML document -->

<HEAD>

<SCRIPT LANGUAGE="JavaScript">

<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->

<!-- Begin
function Login(){
var done=0;
var username=document.login.username.value;
username=username.toLowerCase();
var password=document.login.password.value;
password=password.toLowerCase();
if (username=="member1" && password=="password1") { window.location="page1.html"; done=1; }
if (username=="member2" && password=="password2") { window.location="page2.html"; done=1; }
if (username=="member3" && password=="password3") { window.location="page3.html"; done=1; }
if (done==0) { alert("Invalid login!"); }
}
// End -->
</SCRIPT>

<!-- STEP TWO: Paste this code into the BODY of your HTML document -->

<BODY>

<center>
<form name=login>
<table width=225 border=1 cellpadding=3>
<tr><td colspan=2><center><font size="+2"><b>Members-Only Area!</b></font></center></td></tr>
<tr><td>Username:</td><td><input type=text name=username></td></tr>
<tr><td>Password:</td><td><input type=text name=password></td></tr>
<tr><td colspan=2 align=center><input type=button value="Login!" onClick="Login()"></td></tr>
</table>
</form>
</center>

<p><center>
<font face="arial, helvetica" size="-2">Free JavaScripts provided<br>
by <a href="http://javascriptsource.com">The JavaScript Source</a></font>
</center><p>

<!-- Script Size: 1.60 KB -->

Jona
02-27-2003, 05:37 PM
Try this.


<HEAD>

<SCRIPT LANGUAGE="JavaScript">

<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->

<!-- Begin
function Login(){
var done=0;
var username=document.login.username.value;
username=username.toLowerCase();
var password=document.login.password.value;
password=password.toLowerCase();
if ((username=="member1") && (password=="password1")) { window.location="page1.html"; done=1; }
if ((username=="member2") && (password=="password2")) { window.location="page2.html"; done=1; }
if ((username=="member3") && (password=="password3")) { window.location="page3.html"; done=1; }
if (done==0) { alert("Invalid login!"); }
}
// End -->
</SCRIPT>

<!-- STEP TWO: Paste this code into the BODY of your HTML document -->

<BODY>

<center>
<form name=login>
<table width=225 border=1 cellpadding=3>
<tr><td colspan=2><center><font size="+2"><b>Members-Only Area!</b></font></center></td></tr>
<tr><td>Username:</td><td><input type=text name=username></td></tr>
<tr><td>Password:</td><td><input type=text name=password></td></tr>
<tr><td colspan=2 align=center><input type=button value="Login!" onClick="Login()"></td></tr>
</table>
</form>
</center>

<p><center>
<font face="arial, helvetica" size="-2">Free JavaScripts provided<br>
by <a href="http://javascriptsource.com">The JavaScript Source</a></font>
</center><p>

annasue
02-27-2003, 06:10 PM
I didn't see where Jona answered my question about how/where to add/delete username/password combinations???

Jona
02-27-2003, 06:16 PM
if ((username=="member1") && (password=="password1")) { window.location="page1.html"; done=1; }
if ((username=="member2") && (password=="password2")) { window.location="page2.html"; done=1; }
if ((username=="member3") && (password=="password3")) { window.location="page3.html"; done=1; }


Change, "member1, 2, 3" to your member names. Change, "password1, 2, 3" to your member passwords.

Note: This is not a secure way of password protection. Any true password protection should be done server side, since people can simply view the source and figure out the passwords and answers

Charles
02-27-2003, 06:30 PM
That method will get really ugly when you start adding a lot of people. Try this one on for size. The variable pairs contains what is called an associative array of your user name / password pairs. Just change that to add and subtract people.

By the way, this is way easy to defeat.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Content-Script-Type" content="text/javascript">
<form action="logged.html" onsubmit="return logIn(this)">
<script type="text/javascript">
<!--
var pairs = {user1 : 'password1', user2 : 'password2'};

function logIn (f) {if (pairs [f.userName.value] != f.password.value) {alert ('Invalid Login'); f.userName.value = ''; f.password.value = ''; return false}}
// -->
</script>
<p><label for="userName">User Name</label><br>
<input type="text" id="userName"></p>
<p><label for="password">Password</label><br>
<input type="text" id="password"></p>
<p><input type="submit" value="Log In"></p>
</form>

Jona
02-27-2003, 06:36 PM
Charles, hey I was just helping the guy with what he had. :p You're too good. I have nothing to say....

AdamBrill
02-27-2003, 07:47 PM
One thing that might be worth pointing out is that anyone can click view source and get all of the usernames and passwords. That makes it VERY insecure. The only password protection or login scripts that are secure are the ones that use a server side language. I just thought I would point it out. :)

AdamBrill
02-27-2003, 08:30 PM
Oh, I guess Charles already pointed that out. I missed that part. ;) But here is a php login script that you might try using if your server supports php. It would be MUCH harder(if not impossible) to break.

Jona
02-27-2003, 10:26 PM
Adam, the .zip file you provided. It is password protected. What's the password? I can't unzip it :p

AdamBrill
02-27-2003, 10:36 PM
Jona - I didn't password protect it... I'm not sure why it didn't work for you. Here is the code, though.

The HTML page:
<html>

<head>
<meta http-equiv="Content-Language" content="en-us">
<meta name="GENERATOR" content="Microsoft FrontPage 5.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>New Page 1</title>
</head>

<body>

<form method="POST" action="verify.php">
Username: <input type="text" name="user" size="20"></p>
<p>Password: <input type="password" name="pass" size="20"></p>
<p><input type="submit" value="Submit" name="B1"><input type="reset" value="Reset" name="B2"></p>
</form>

</body>

</html>

the PHP code:
<?PHP

$usernames = array("Bob","Jim","Jack");
$passwords = array("test","test1","test2");
$x=0;
do
{
if($usernames[$x]==$user)
{
if($passwords[$x]==$pass)
{
header("location:http://www.yahoo.com");
}
}
$x++;
} while($usernames[$x]);
echo ("<center><b><font size=5>Access Denied</font></b></center>");
?>

Make sure you name the php file verify.php

annasue
02-28-2003, 01:13 PM
Does anyone know of a simple script for logging in with username/password that reads an ordinary delimited .txt file for valid username/password combinations?

AdamBrill
02-28-2003, 04:03 PM
That could be done with php, but then it isn't as secure. Then people can just download the .txt file and they have all of the usernames and passwords. I realize that they would have to find the .txt file, but that isn't really that hard to do if they really want to. That is why I made it like I did. One thing that I could do is take if from an external .php file. Would that be better?

pyro
02-28-2003, 04:24 PM
Or, if your server supports databases, PHP with mySQL would be work quite nicely...