-7

The question I am asking has been asked here but was asked pretty badly and resulted in the problem not being resolved. What the guy that asked that question is experiencing is exactly what I am experiencing, I assume he is following the same YouTube tutorial as me. Anyway here is all of the PHP / HTML code I have written to far:

<?php
session_start();
?>

<?php
$form = "<form action='./login.php' method='post'>
<table>
<tr>
<td>Username:</td>
<td><input type='text' name='user' /></td>
</tr>

<td>Password:</td>
<td><input type='password' name='password' /></td>
</tr>

<td></td>
<td><input type='submit' name='loginbtn'  value='Login'/></td>
</tr>
</table>

</form>";

if ($_POST ['loginbtn']){
$user = $_POST['user'];
$user = $_POST['password'];


if ($user){ 
if ($password){ 
echo "$user - $password <hr /> $form";
}
else
echo "you must enter your password $form";

}
else
echo "you must enter your username $form";

}
else
echo $form;

?>

I've tried adding a variable to if ($user){ and if ($password){ but that did nothing. Anyone know what the issue could be?

Community
  • 1
  • 1
Roy Smith
  • 1
  • 1
  • 2

3 Answers3

1
$user = $_POST['user'];
$user = $_POST['password'];

$password is not properly defined, and you are using it in the if below

cornelb
  • 6,046
  • 3
  • 19
  • 30
1
$user = $_POST['user'];
$user = $_POST['password'];

This... you are assigning both user and password to the same variable ($user). That is why you have undefined variable password.

Also use htmlentities to prevent code injection:

$user = htmlentities($_POST['user']);
$password = htmlentities($_POST['password']);
cssyphus
  • 37,875
  • 18
  • 96
  • 111
0

Sorry, that doesn't look like a very good tutorial. For one thing, I see that you create loginID and password fields on the page, but at what point is a user submit received? And where is the code for those values to be passed to PHP, evaluated, and returned?

I suggest these other tutorials instead:

phpAcademy - Register and Login - OOP version
phpAcademy - Register and Login - Procedural version

theNewBoston - Project Lisa (adv)

From your code, I think you will be most comfortable with the procedural tutorial from phpAcademy, but you should really learn the OOP one.


Other comments:

(removed b/c cornelb beat me to the punch -- see his answer.)

cssyphus
  • 37,875
  • 18
  • 96
  • 111