Page 1 of 1

Login Script Help

Posted: Sun Jan 22, 2006 1:26 pm
by nickman013
hey!

i have a password script that works right now

Code: Select all

<? 
function showForm() { 
echo "<form method=post action=/pages/login.php><br>admin id# :<br> <input type=password name=pw><br /> 
<input type=submit> 
</form>"; 
} 

$content = "welcome";
$wrong = "<html><font size=-1 color=red><b>THE PASSWORD YOU ENTERED IS INCORRECT</b></font></html>"; 
//check if something was entered into field 
if(!empty($HTTP_POST_VARS['pw'])) { 
if($HTTP_POST_VARS['pw'] == 'mypassword') { 
echo $content;
} 
else { 
showForm(); 
echo $wrong; 
} 
} 
else { 
showForm(); 
}
?>
we will name this login script #1

basically it starts out with just the password form, and if its incorrect it will display password is incorrect..

i would like to upgrade my login form, right now my code is

Code: Select all

<?
if ($_POST['user']=='bob' && $_POST['pass']=='bobs') {header('Location:http://www.google.com');
}
elseif($_POST['user']=='john' && $_POST['pass']=='johns') {header('Location:http://www.google.com');
}
else {echo "wrong user name or password";}
?>
<html>
<div align=center><form action="test.php" method="post"><font size=2><b>Username:
<input type=text size=5 maxlength=5 name=user><br>
<font size=2><b>Password:
<input type=password size=5 maxlength=5 name=pass><br><input type=submit value=Login.>
</form>
</div>
we will name this login script #2

is there any way to make login script #2 like login script#1 where it doesnt display user name or password is incorrect before something is submitted, get what i mean?

so what i want to basically do

1. Make a login script with 2 users.
2. If the password or username is wrong, it will display "password or username is wrong.."
3. I dont want it to view "password or username is wrong..." when the form has not been submitted yet
4. I want it to process on the same page.


thank you!!! :D


special thanks to jshpro2, for helping me with the new login script

Posted: Sun Jan 22, 2006 1:30 pm
by Zoram
change:

Code: Select all

else {echo "wrong user name or password";}
to:

Code: Select all

elseif($_SERVER['REQUEST_METHOD'] == 'POST') {echo "wrong user name or password";}

Posted: Sun Jan 22, 2006 1:32 pm
by nickman013
I love you.!!!
thank you so much

Posted: Sun Jan 22, 2006 1:36 pm
by raghavan20

Code: Select all

<? 
if (isset($_POST['user']) && isset($_POST['pass'])){
	if ($_POST['user']=='bob' && $_POST['pass']=='bobs'){
		header('Location:http://www.google.com'); 
	} 
	elseif($_POST['user']=='john' && $_POST['pass']=='johns'){
		header('Location:http://www.google.com'); 
	} 
	else{
		echo "wrong user name or password";
	} 
}
?>

Posted: Sun Jan 22, 2006 1:45 pm
by nickman013
they both worked, so now i am having one problem, if one of the users login, it says, the header was already sent out, my script is

Code: Select all

<? 
$login = "<html>
<div align=center><form action=test.php method=post><font size=2><b>Username:
<input type=text size=5 maxlength=5 name=user><br>
<font size=2><b>Password:
<input type=password size=5 maxlength=5 name=pass><br><input type=submit value=Login.>
</form>
</div></html>";
echo "$login";
if (isset($_POST['user']) && isset($_POST['pass'])){ 
if ($_POST['user']=='bob' && $_POST['pass']=='bobs'){ 
header('Location:http://www.google.com'); 
} 
elseif($_POST['user']=='john' && $_POST['pass']=='johns'){ 
header('Location:http://www.google.com'); 
} 
else{ 
echo "<html><br><div align=center><font size=-1 color=red>WRONG USERNAME OR PASSWORD</FONT></div></HTML>"; 
} 
} 
?>

Posted: Sun Jan 22, 2006 2:19 pm
by nickman013
is there anything else i can put instead of header?

Posted: Sun Jan 22, 2006 2:27 pm
by Zoram
You cannot send a header when you have outputed to the screen. you can use output buffering of just move the login script below the processing. any processing where you are going to be redirecting should be done as much as possible before you output anything.

BTW: you are sending two <HTML> tags on the event that the login is incorrect.

Posted: Sun Jan 22, 2006 2:38 pm
by nickman013
ok so is there a way to make it so they both go to a page , while only putting one header().

ty

Posted: Sun Jan 22, 2006 2:43 pm
by Zoram
You want them both to go to the same page? reguardless of which user logs in? then do:

Code: Select all

<?
$error= '';
if (isset($_POST['user']) && isset($_POST['pass'])){
if (($_POST['user']=='bob' && $_POST['pass']=='bobs') || ($_POST['user']=='john' && $_POST['pass']=='johns')){
header('Location:http://www.google.com');
} else {
$error = "<br><div align=center><font size=-1 color=red>WRONG USERNAME OR PASSWORD</FONT></div>";
}

echo "
<html>
<body><div align=center><form action=test.php method=post><font size=2><b>Username:
<input type=text size=5 maxlength=5 name=user><br>
<font size=2><b>Password:
<input type=password size=5 maxlength=5 name=pass><br><input type=submit value=Login.>
</form>
</div>"
 . $error . "
</body></html>";
}
?>

Posted: Sun Jan 22, 2006 3:03 pm
by nickman013
the page came up blank

Posted: Sun Jan 22, 2006 3:12 pm
by nickman013
ok, i tried to edit this script my self, and BAM!, it worked

the final code is

Code: Select all

<? 
if (isset($_POST['user']) && isset($_POST['pass'])){ 
if (($_POST['user']=='bob' && $_POST['pass']=='bobs') || ($_POST['user']=='john' && $_POST['pass']=='johns')){ 
header('Location:http://www.google.com'); 
} else { 
$error = "<div align=center><font size=-1 color=red>WRONG USERNAME OR PASSWORD</FONT></div>"; 
echo "$error";
} 
}
$form = " 
<html> 
<body><div align=center><form action=test.php method=post><font size=2><b>Username: 
<input type=text size=5 maxlength=5 name=user><br> 
<font size=2><b>Password: 
<input type=password size=5 maxlength=5 name=pass><br><input type=submit value=Login.> 
</form> 
</div></body></html>";
echo "$form";
?>
thank you everyone for the help!

:D

now i got to get sessions working! :oops: