title: Login using Mysql database username and password
body: <p>i'm trying to make my php scripts protected pages, and so when adding a login i want it to check the username and password inputs against the database username and password and if they match it will allow them to view the protected pages until logout. I've been able to set up the protected page &amp; logout based on a dummy login username and password now i want to connect it to the database users. This is my html &amp; php code. Any help would be appreciated :)</p>
  
  <p>HTML:</p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;
  &lt;title&gt;Login&lt;/title&gt;
  
  &lt;/head&gt;
  &lt;body&gt;
  &lt;form method="post" action="login.php"&gt;
   &lt;table border="1" cellpadding="2"&gt;
  &lt;caption&gt;
  Staff Login
  &lt;/caption&gt;
  &lt;tr&gt;
      &lt;td&gt;&lt;label for="username"&gt;Username&lt;/label&gt;&lt;/td&gt;
      &lt;td&gt;&lt;input type="text" name="username" cols="45" rows="5" tabindex="2"/&gt;&lt;/td&gt;
  &lt;/tr&gt;
      &lt;tr&gt;
      &lt;td&gt;&lt;label for="password"&gt;Password&lt;/label&gt;&lt;/td&gt;
      &lt;td&gt;&lt;input type="password" name="password" cols="45" rows="5" tabindex="2"&gt;&lt;/textarea&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
      &lt;td colspan="2" align="center"&gt;&lt;input type="submit" value="Login" /&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;/table&gt;
  
  &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>PHP:</p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;
  &lt;title&gt;Login&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
  
  &lt;?php
  
  // Check if username and password are correct
  
  
  if ($_POST["username"] == 'username' &amp;&amp; $_POST["password"] == 'password') {
  include 'connection.php'; 
  // If correct, we set the session to YES
    session_start();
    $_SESSION["Login"] = "YES";
    echo "You are now logged in.&lt;/br&gt;&lt;/br&gt;";
    echo "&lt;a href='list-contacts.php'&gt;Proceed to contacts...&lt;/a&gt;";
  
  }
  else {
  
  // If not correct, we set the session to NO
    session_start();
    $_SESSION["Login"] = "NO";
    echo "Incorrect username and / or password.&lt;/br&gt;&lt;/br&gt;";
    echo "&lt;a href='login.html'&gt;Return to login&lt;/a&gt;";
  
  }
  
  ?&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>