-5

I'm creating a login system for a site I'm developing and this error appears when I enter a password into the password field:

Notice: Undefined variable: password in C:\xampp\htdocs\myfiles\login.php on line 51 you must enter your password

This is the code on line 51: if ($password){

This is some of the code surrounding the line:

Line 50: if ($user){

Other code surrounding the line:

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

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

}
else
echo $form;

Anyone know what the problem is or could be?

tarzanbappa
  • 4,930
  • 22
  • 75
  • 117
  • You have not set/initialized variable password. Secondly it is not an error. It is a notice. In place of "if ($password){" use "if (isset($password) && !empty($password)){" – gurudeb Jan 26 '14 at 16:50

3 Answers3

0

Well, the only reason you're getting this error, is because you variable isn't defined. Try to use isset(), so:

if (isset($user) && isset($password)){
Eisa Adil
  • 1,743
  • 11
  • 16
  • Thanks but now when I enter a password and username this message appears even though I have entered a password: you must enter your password. Also when I only enter my username this message appears: you must enter your username – Gary Daniels Jan 26 '14 at 16:53
  • 1
    Show your full code. These types of questions always lead to potential downvotes and/or problems (a proverbial can of worms, as I call it). You obviously have more than 51 lines of code. Showing a dozen lines or so of code with the most important parts missing, is not enough. It's like trying to play golf in the dark, hitting the ball and hopefully it will land in the right hole. @GaryDaniels --- Although I did `NOT` downvote your question, that is most likely the reason why you got downvoted. – Funk Forty Niner Jan 26 '14 at 17:27
0

You have not set/initialized variable password. Secondly it is not an error. It is a notice.

In place of

if ($password){ 

use

if (isset($password) && !empty($password)){ 

Similar for $user variable as well. isset() checks if the variable is defined or not. !empty() checks for empty value.

PHP Manual links:

http://www.php.net/isset

http://www.php.net/empty

Different types of error reporting in PHP (to know the details about error and notice): http://www.w3schools.com/php/func_error_reporting.asp

gurudeb
  • 1,856
  • 22
  • 29
0

You need to check if your variables are defined

if (isset($user) && isset($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;
bastienbot
  • 123
  • 1
  • 14
  • I cant do much without the full code to be honest... I don't where those two else come from, don't where the if are. Show me some code. – bastienbot Jan 26 '14 at 18:04
  • note that if you get those variable from a form, you need to use them accordingly to the method the form uses to send them, `$_POST['user']`, or `$_GET['user']` instead of user. – bastienbot Jan 26 '14 at 18:12