Jump to content

Php & Mysql Error: Undefined Index: Username


tinfanide
 Share

Recommended Posts

I've got four php pages for a login system and a database on SQL. index.php

<html><form action="login.php" method="POST">Username: <input type="text" name="username" /> <br />    Password: <input type="password" name="password" /> <br />    <input type="submit" value="Log in" /></form></html>

login.php

<?phpsession_start();$username = $_POST['username'];$password = $_POST['password'];if($username&&$password){$connect = mysql_connect("localhost","root","") or die("couldn't connect!");mysql_select_db("phplogin") or die("couldn't find the database!");$query = mysql_query("SELECT * FROM users WHERE username='$username'");$numrows = mysql_num_rows($query);if ($numrows!=0){  while ($row = mysql_fetch_assoc($query)){   $dbusername = $row['username'];   $dbpassword = $row['password'];   }   if ($username==$dbusername&&$password==$dbpassword){   echo "You're in! <a href='member.php'>Click</a> here to enter the memeber page.";   $_SESSION['username']=$username;   } else {    echo "Incorrect password!";    }   } else {   die("That user doesn't exist!");   }  } else {  die("Please enter the username and the password!");  }?>

member.php

<?phpsession_start();if ($_SESSION['username']){echo 'Welcome, '.$_SESSION['username'].'!';echo '<br />';echo "<a href='logout.php'>Log out</a>";} else {  die("You must be logged in!");  }?>

logout.php

<?phpsession_start();session_destroy();echo "You've been logged out. <a href='index.php'>Click here</a>to return";?>

But the error is index.phpI log inOK login.phpYou're in! Click here to enter the memeber page. I click 'Click'Go to member.php member.phpWelcome, abc!Log out I click "Log out" logout.phpYou've been logged out. Click hereto return I click "Click here"return to index.php And I type "http://localhost/_test/phpLogin/member.php" to try to go back to member.php againIt says: Notice: Undefined index: username in C:\xampp\htdocs\_test\phpLogin\member.php on line 5You must be logged in! So, what is undefined index: username?

Link to comment
Share on other sites

I believe because you have logged out and when you did that, you destroyed the sessions. So when you tried to go back into member.php directly, you're going to get that error because $_SESSION['username'] was destroyed when you logged out using logout.php. Just to add: I recommend when you're checking for variables if they are set or not(true or not), to use the empty() function. For example, instead of the above, try this:

if(!empty($username)&&!empty($password)){$connect = mysql_connect("localhost","root","") or die("couldn't connect!");mysql_select_db("phplogin") or die("couldn't find the database!");$query = mysql_query("SELECT * FROM users WHERE username='$username'");$numrows = mysql_num_rows($query);....rest of code

What !empty() does above is makes sure there is no empty value in $username, like 0, null. In case a user does not enter a value, it will be caught. If you use if($username), it will come out as true because it's a declared variable. This goes for isset() too. Try not to use isset() function for the above. Example: if(isset($username)) because it's going to return true even if there is nothing inside $username. For more info check out the php.net for empty() and isset().

Edited by Don E
Link to comment
Share on other sites

It's strange and I asked this question cos I didn't see the video tutorial having such an error message.But I get what ya meant now.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...