title: Login index form is not working with login.php if i use header() function
body: <p>I am beginner in php.
  I am trying to make a login form(index1.php).
  It works fine if i redirect it to login.php using "action" attribute in form tag.
  But i also want some validations on same index form.So, i am redirecting it to "login.php" using header function and not in "action" attribute.</p>
  
  <p>No matter if i enter correct username and password or not, it is giving me errors of invalid variables '$username' and '$password' while executing the last else too of login.php(//that user doesnt exist).
  Please help me in knowing whats wrong with my code and what should i do to make it run with login.php?</p>
  
  <p>Here is the code of index1.php</p>
  
  <pre><code>  &lt;!DOCTYPE HTML&gt;
  &lt;html&gt;
  &lt;head&gt;&lt;br&gt;
  &lt;style&gt;
  .error {color: #FF0000;}
  &lt;/style&gt;
  &lt;/head&gt;
  &lt;body style="font-family: verdana, sans-serif;";&gt;   
  &lt;hr /&gt;
  &lt;div style="width: 77%; height: 300px; padding: 30px; border: 5px solid #e3e3e3; background-color: #F8F8F8 ; color: #000; margin: 100px;" align="center"&gt;
   &lt;br&gt;
  &lt;?php
  
   $username=$password="";
   $Err="";
  
   if($_SERVER['REQUEST_METHOD']== "POST")
   {  
  
       $valid = true;
  
       if ((empty($_POST["username"])) || (empty($_POST["password"])))
      {
        $Err = "Please Enter your username and password";
        $valid = false; 
      }
  
     else 
      {
  
        $username = test_input($_POST["username"]);
        $password = test_input($_POST["password"]);
      }
  
  
     if($valid)
     { 
       header("location:login.php");
     }
  
  
   } 
  
   function test_input($data) 
  {
     $data = trim($data);
     $data = stripslashes($data);
     $data = htmlspecialchars($data);
     return $data;
  }
  
  ?&gt;
  
   &lt;span class="error"&gt;&lt;?php echo $Err; ?&gt;&lt;/span&gt;
   &lt;h2&gt;Login&lt;/h2&gt;
   &lt;form action="menubar.php?page=index1" method="post"&gt;
        &lt;font size="3"&gt;Username:&lt;input type="text" name="username" style="padding: 4px;"/&gt;&lt;p&gt;
        Password: &lt;input type="password" name="password" style="padding: 4px;"/&gt;&lt;p&gt;
                   &lt;input type="submit" value="Login" style="padding: 4px; width: 5em; height: 1.9em;"/&gt;&lt;/font&gt;
      &lt;/form&gt;
       &lt;h5&gt; No Account?&lt;a href="menubar.php?page=register"&gt;Register!&lt;/a&gt;&lt;/h5&gt;
      &lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>And here is the code of login.php.</p>
  
  <pre><code>&lt;?php
  
  session_start();
  
  $username=$_POST["username"];
  $password=$_POST["password"];
  $message[]="Fill up both username and password";
  
      $connect=mysql_connect("localhost","root","") or die("Couldnt connect to Database");
      mysql_select_db("login") or die("Couldnt find Database");
      $query = mysql_query("SELECT * FROM users WHERE username='$username'");
  
      $numrows = mysql_num_rows($query);
  
      if($numrows!==0)//username exists in database
      {
          while($row = mysql_fetch_assoc($query))
          {
              $dbusername = $row['username'];
              $dbpassword = $row['password'];
          }
          if($username==$dbusername&amp;&amp;$password==$dbpassword)//valid username and password
          {
  
              $_SESSION['username'] = $username;
             header("Location: menubar.php?page=home");
          }
          else//incorrect password or username
          die("incorrect password or username");
  
      }
      else//that user doesnt exit
      die("that user doesnt exit");//
  
  
  ?&gt;
  </code></pre>
  
  <p>Okay, Now i used 'sqli' instead</p>
  
  <p>Here is that updated login.php code</p>
  
  <pre><code>&lt;?php
  
  session_start();
  
  $username=$_POST["username"];
  $password=$_POST["password"];
  $message[]="Fill up both username and password";
  
      $connect=mysqli_connect("localhost","root","") or die("Couldnt connect to Database");
      mysqli_select_db($connect,'login') or die("Couldnt find Database");
      $query = mysqli_query("SELECT * FROM users WHERE username='$username'");
  
      $numrows = mysqli_num_rows($query);
  
      if($numrows!==0)//username exists in database
      {
          while($row = mysqli_fetch_assoc($query))
          {
              $dbusername = $row['username'];
              $dbpassword = $row['password'];
          }
          if($username==$dbusername&amp;&amp;$password==$dbpassword)//valid username and password
          {
  
              $_SESSION['username'] = $username;
             header("Location: menubar.php?page=home");
          }
          else//incorrect password or username
          die("incorrect password or username");
  
      }
      else//that user doesnt exit
      die("that user doesnt exit");//
  
  
  ?&gt;
  </code></pre>
  
  <p>Now after updating i am not getting errors of invalid variables .
  But it still not going on correctly.No matter whether i enter correct username and password or not, it is executing the innermost if i.e. (//valid username and password)</p>
  
  <p>Thanks....</p>