Login Script Help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Login Script Help

Post 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
User avatar
Zoram
Forum Contributor
Posts: 166
Joined: Sun Aug 18, 2002 3:28 pm
Location: Utah
Contact:

Post 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";}
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post by nickman013 »

I love you.!!!
thank you so much
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post 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";
	} 
}
?>
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post 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>"; 
} 
} 
?>
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post by nickman013 »

is there anything else i can put instead of header?
User avatar
Zoram
Forum Contributor
Posts: 166
Joined: Sun Aug 18, 2002 3:28 pm
Location: Utah
Contact:

Post 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.
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post by nickman013 »

ok so is there a way to make it so they both go to a page , while only putting one header().

ty
User avatar
Zoram
Forum Contributor
Posts: 166
Joined: Sun Aug 18, 2002 3:28 pm
Location: Utah
Contact:

Post 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>";
}
?>
Last edited by Zoram on Sun Jan 22, 2006 3:06 pm, edited 2 times in total.
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post by nickman013 »

the page came up blank
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post 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:
Post Reply