I am building on a previous thread which got the user name by now asking for password as well.
But for some reason it doesn't work, it jumps right to the default.
Thanks for looking

Here is the code

<?php 
$usern1 = Joe;
$pass1 = abc;
$usern2 = Jim;
$pass2 = lmn;
$usern3 = Jack;
$pass3 = xyz;
if ($_POST[$user] == $usern1 && $_POST[$password] ==  $pass1)
echo("Joe you are now logged in");
elseif ($_POST[$user] == $usern2 && $_POST[$password] ==  $pass2)
echo("Jim you are now logged in");
elseif ($_POST[$user] == $usern3 && $_POST[$password] ==  $pass3)
echo("Jack you are now logged in");
else {
echo("Login Failed");
}
?>

Recommended Answers

All 13 Replies

Try putting single or double quotes around your values like this:

$usern1 = "Joe";
$pass1 = "abc";
$usern2 = "Jim";
$pass2 = "lmn";
$usern3 = "Jack";
$pass3 = "xyz";

The form you are using, is it contained on this file, or another file?
Can you also paste the code for that form, this will help us help you better:)

Hi Riche thanks for the suggetion, still not working so here is the form that calls the php page.

<form method="post" action="login.php" name="userID" >
<input type="hidden" name="env_report" value="REMOTE_HOST,REMOTE_ADDR">
<div align="center">  
<h4></center>Please enter your<br /> user name and Password:</h4></font>
</div>
</td>
</tr>
<tr>
<td align ="center" bordercolor="#000000">
User Name: 
<input type="text" name="user"  />
</td>
</tr>
<tr>
<td align ="center" bordercolor="#000000"> 
Password: &nbsp;
<input type="password" name="password" /><br />
</td>
</tr>
<tr>
<td align ="center" bordercolor="#000000"> 
<input type="submit" name="send" value="Submit" />
</td>
</tr>
</table>
</form>
<?php 
$usern1 = "Joe";
$pass1 = "abc";
$usern2 = "Jim";
$pass2 = "lmn";
$usern3 = "Jack";
$pass3 = "xyz";
if ($_POST[$user] == $usern1 && $_POST[$password] ==  $pass1)
echo("Joe you are now logged in");
elseif ($_POST[$user] == $usern2 && $_POST[$password] ==  $pass2)
echo("Jim you are now logged in");
elseif ($_POST[$user] == $usern3 && $_POST[$password] ==  $pass3)
echo("Jack you are now logged in");
else {
echo("Login Failed");
}
?>

try replacing your php with this:

<?php 
$usern1 = "Joe";
$pass1 = "abc";
$usern2 = "Jim";
$pass2 = "lmn";
$usern3 = "Jack";
$pass3 = "xyz";
if ($_POST[$user] == $usern1 && $_POST[$password] ==  $pass1)
{
echo("Joe you are now logged in");
}
elseif ($_POST[$user] == $usern2 && $_POST
[$password] ==  $pass2)
{
echo("Jim you are now logged in");
}
elseif ($_POST[$user] == $usern3 && $_POST
[$password] ==  $pass3)
{
echo("Jack you are now logged in");
} else {
echo("Login Failed");
}
?>

Hi Aussie
Thanks for the advice but still no luck?

change $_POST[$user] and $_POST[$password] to $_POST[user] and $_POST[password]

in other words drop the $

slight typo of mine it should be $_POST and $_POST

Hi Aussie,

Success!! I've posted the code below so that other members just starting out with php can look at it.
I have another question please, so that I can build on this script, in fact I have two.
1) Can this be placed in a switch statement, instead of elseif?
2) How can I place the info into a log file *.txt.

I do appreciate the help with this script, Thanks.

<?php
$usern1 = "Joe";
$pass1 = "abc";
$usern2 = "Jim";
$pass2 = "lmn";
$usern3 = "Jack";
$pass3 = "xyz";
if ($_POST['user'] == $usern1 && $_POST['password'] ==  $pass1)
{
echo("Joe you are now logged in");
}
elseif ($_POST['user'] == $usern2 && $_POST ['password'] ==  $pass2)
{
echo("Jim you are now logged in");
}
elseif ($_POST['user'] == $usern3 && $_POST ['password'] ==  $pass3)
{
echo("Jack you are now logged in");
} else {
echo("Login Failed");
}
?>

well for question 2:

$myFile = "testFile.txt";
$filehandle = fopen($myFile, 'w') or die("can't open file");
$stringData = "Bobby Bopper\n";
fwrite($filehandle, $stringData);
$stringData = "Tracy Tanner\n";
fwrite($filehandle, $stringData);
fclose($filehandle);

fwrite writes a string to the file, usually best to stick it into a variable first, flcose closes the file writing process.
fopen opens the file, now the second parameter is special.
If you want to just write to the file each time the file is opened (which means you overwrite anything previously there) then set the second parameter to w, if you want to append (write to the end of the file) then make the second parameter a or if you just want to read the data then use r
I will get back to you on the first question in a bit when i have woken up

Morning Aussie,

ROFLOL

Thanks for a detailed description, I will go and play around with it for a while.

While I'm writing can you recommend any good books for newbies?

I wouldn't call it detailed :P as for a nice book for newbies hmm... try phpsolutions or php & mysql programming for beginners, both available on Amazon at a nice and cheap price ;)
Let us know if you need anything else
But i'm sure if i can find it i will pm you a link to a copy of the php & mysql book off my server

This here is a good book for beginners, I used it when I started, and still refer back to it every now and then. It is online and free, you can find it here Practical PHP

richie513:)

commented: Decent guy and genuine +1

Thanks for the link Richie, It's bookmarked!

Your welcome whoisit:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.