title: User authentication for accessing pages
body: <p>I'm working on page authentication. It can login already, but I want it to make user authentication on other pages a swell if someone tries to access pages through URL. If the person is not a logged in user, redirect that person to the login page. I tried it by working with sessions but it doesn't work. I'm following MVC structure </p>
  
  <p>Here is how I did it</p>
  
  <p>My loginController</p>
  
  <pre><code>    &lt;?php
  //LoginController
  if($_POST)
  {
      if(isset($_POST['submit']) AND $_POST['submit'] == "login")
      {
          $username = $_POST['username'];
          $password = $_POST['password'];
          try
          {
              include '../model/Login.php';
              $login = new Login($db, $username, $password);
  
              if($login == TRUE)
              {
                  session_start();
                  $_SESSION['username'] = $username;
                  header("Location:../index.php");
              }
  
          }
          catch (Exception $exc)
          {
              echo $exc-&gt;getMessage();
          }
  
      }
  }
  </code></pre>
  
  <p>My index controller( for main page)</p>
  
  <pre><code>&lt;?php
  include 'model/Database.php';
  session_start();
  //Checks if the user is logged in.
  if(!isset($_SESSION['username'])) {
  //echo"&lt;h2&gt;You have no access to this page!";
      include 'view/login.php';
      die();
  }
  include 'c:/wamp/www/mvc/model/Display.php';
  
  $displayPatients = new Display($db);
  $dataDisplay = $displayPatients-&gt;getData();
  include 'view/Main.php';
  ?&gt;
  </code></pre>