title: Kind Assistance with Displaying Login Errors on Home Page
body: <p>I am testing the login system for a study planner that I am building. As for now, the application seems to let me log in and logout okay but the current problem I have is that it won’t display an error messages (<code>$error</code> variable) on the index.php (which were initialised in the login.php file) when it is supposed to. So, I am wondering if someone could please point me in the right direction, as I cannot seem to figure out what I am doing wrong.</p>
  
  <p>I could post the whole files but apart from the fact that I don’t want to inundate a potential helper, I have included the files which I believe the most important, that is, the login.php and index.php but I can include the others too if need be.</p>
  
  <p>Thanking you in advance.</p>
  
  <p>Login.php:</p>
  
  <pre><code>&lt;?php
  
  // Start session
  session_start();
  // Variable to store error message
  $error = '';
  // If the login form (Note that the 'submit' refers to the 'name' attribute of the login form) has been submitted...
  if (isset($_POST['submit'])) {
      // If username or password is not provided...
      if (empty($_POST['username']) || empty($_POST['password'])) {
          // ...tell user that login details are invalid.
          $error = "Please fill in both your username and your password";
          // Else...
      } else {
          // ...put the provided username and password in variables $username and $password, respectively
          $username = $_POST['username'];
          $password = $_POST['password'];
          // Establish connection to the server
          $mysqli = mysqli_connect("localhost", "root", "");
          // set up measures to counter potential MySQL injections
          $username = stripslashes($username);
          $password = stripslashes($password);
          $username = mysqli_real_escape_string($mysqli, $username);
          $password = mysqli_real_escape_string($mysqli, $password);
          // Select Database
          $db = mysqli_select_db($mysqli, "p00702");
          // SQL query to fetch information of registerd users and find user match.
          $query = mysqli_query($mysqli, "SELECT * from logins WHERE password='$password' AND username='$username'");
          // Return the number of rows of the query result and put it in $rows variable
          $rows = mysqli_num_rows($query);
          // If rows are equal to one...
          if ($rows == 1) {
              // Initialize session with the username of the user...
              $_SESSION['login_user'] = $username;
              // ...and redirect to the homepage.
              header("Location: welcome.php");
              // Make sure that codes below do not execut upon redirection.
              exit;
          // Else, 
          } else {
              // redirect user to the home page (index.php)
              header("Location: index.php");
              // and tell user that the login credentials are invalid.
              $error = "Username or Password is invalid";
          }
          // ...and close connection
          mysqli_close($mysqli);
      }
  }
  </code></pre>
  
  <p>Index.php:</p>
  
  <pre><code>&lt;?php
  include ('login.php');
  ?&gt;
  
  &lt;!DOCTYPE html&gt;
  &lt;html&gt;
      &lt;head&gt;
          &lt;meta charset="UTF-8"&gt;
          &lt;link rel="shortcut icon" type="image/png" href="/images/favicon.png"/&gt;
          &lt;title&gt;Just-Read&lt;/title&gt;
          &lt;link rel="stylesheet" type="text/css" href="css/styles.css"&gt;
      &lt;/head&gt;
      &lt;body&gt;
          &lt;!-- Link to external JavaScript file --&gt;
          &lt;script src="javascript/validator.js"&gt;&lt;/script&gt;
          &lt;div id="container"&gt;
              &lt;div id="header"&gt;
                  &lt;!-- Web site Logo --&gt;
                  &lt;div class="logo"&gt;
                      &lt;img src="images/logo.png" width="128" height="93.5" alt="Logo" class="logo"/&gt;
                  &lt;/div&gt;
                  &lt;div id="logoText"&gt;
                      &lt;h1&gt;Just Read&lt;/h1&gt;
                  &lt;/div&gt;
                  &lt;div id="registerLink"&gt;
                      &lt;!-- Registration link --&gt;
                      &lt;h5&gt;New around here? &lt;a href="registration.php"&gt;Register&lt;/a&gt;&lt;/h5&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
              &lt;div id="leftColumn"&gt;
                  &lt;h4&gt;Your study companion&lt;/h4&gt;
              &lt;/div&gt;
              &lt;div id="rightColumn"&gt;
                  &lt;!-- Authentication Form --&gt;
                  &lt;form name="authentication" action="login.php"  autocomplete="on" method="POST"&gt;
                      &lt;div class="login"&gt;
                          &lt;label&gt;&lt;b&gt;Username*&lt;/b&gt;&lt;/label&gt;
                          &lt;input type="email" id="username" name="username" placeholder="Enter your email" autofocus value=""/&gt;
                          &lt;label&gt;&lt;b&gt;Password*&lt;/b&gt;&lt;/label&gt;
                          &lt;input type="password" id="password" name="password" placeholder="Enter your password" value=""/&gt;
                          &lt;button name="submit" type="submit"&gt;Log in&lt;/button&gt;
                          &lt;div id="mandatoryFields"&gt;
                              &lt;h4&gt;* Mandatory Fields&lt;/h4&gt;
                          &lt;/div&gt;
                      &lt;/div&gt;
                      &lt;span&gt;
                          &lt;?php
                          echo $error;
                          ?&gt;
                      &lt;/span&gt;
                  &lt;/form&gt;
              &lt;/div&gt;
              &lt;div id="footer"&gt;
                  &lt;div id="footerText"&gt;
                      Copyright &amp;copy; 2017, Chizzy Meka.
                  &lt;/div&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>