title: PHP dynamic login and logout using sessions
body: <p>I am just learning php and have an assignment to create a dynamic login/logout page using sessions.  I have created it, but the logout script is not working.  If I login and then logout, the button still says logout and the welcome message with my name appears as if the session is not being terminated.  I have tried everything I can think of to fix this and nothing works.  My question is: how can I fix my code so that the logout script works?  If anyone can help me figure this out, I would sure appreciate it.  I have a template page, which is where I have placed the following code:</p>
  <pre><code>&lt;!-- Contents of template.php --&gt;
  &lt;?php
  if(isset($_POST['logout'])) {
      $msg = 'You are logged out.';
      foreach ($_SESSION as $field =&gt; $value) {
          unset($_SESSION[$field]);
      }
      session_destroy();
      header(&quot;Location: login.php?msg=$msg&quot;);
      exit;
  }
  if(isset($_SESSION['memberID'])) {
      $loginButton = '&lt;button class=&quot;btn btn-outline-primary&quot; name=&quot;logout&quot; type=&quot;submit&quot;&gt;Logout&lt;/button&gt;';
  
  } else {
      $loginButton = '&lt;button class=&quot;btn btn-outline-primary&quot; name=&quot;login&quot; type=&quot;submit&quot;&gt;Login&lt;/button&gt;';
  }
  
  if(isset($_COOKIE['firstname'])){
      $firstname = $_COOKIE['firstname'];
  } else {
      $firstname = 'Guest';   
  }
  echo &quot;&lt;p&gt;Welcome back, $firstname.&lt;/p&gt;&quot;;
  ?&gt;
  
  &lt;!doctype html&gt;
  &lt;html lang=&quot;en&quot;&gt;
  
    &lt;head&gt;
      &lt;meta charset=&quot;utf-8&quot;&gt;
      &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1, shrink-to-fit=no&quot;&gt;
      &lt;title&gt;&lt;?php echo $pageTitle; ?&gt;&lt;/title&gt;
  
      &lt;!-- Bootstrap CSS --&gt;
      &lt;link rel=&quot;stylesheet&quot; href=&quot;https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css&quot;&gt;
      &lt;link rel=&quot;stylesheet&quot; href=&quot;styles.css&quot;&gt;   
    &lt;/head&gt;
    &lt;body&gt;
      &lt;div class=&quot;container&quot;&gt; 
          &lt;nav class=&quot;navbar navbar-expand-lg navbar-dark bg-dark&quot;&gt;
              &lt;a class=&quot;navbar-brand&quot; href=&quot;#&quot;&gt;&lt;/a&gt;
                  &lt;button class=&quot;navbar-toggler&quot; type=&quot;button&quot; data-toggle=&quot;collapse&quot; data- target=&quot;#navbarNavAltMarkup&quot; aria-controls=&quot;navbarNavAltMarkup&quot; aria-expanded=&quot;false&quot; aria-label=&quot;Toggle 
  navigation&quot;&gt;
                      &lt;span class=&quot;navbar-toggler-icon&quot;&gt;&lt;/span&gt;
                  &lt;/button&gt;
      &lt;div class=&quot;collapse navbar-collapse&quot; id=&quot;navbarNavAltMarkup&quot;&gt;
      &lt;div class=&quot;navbar-nav&quot;&gt;
        &lt;a class=&quot;nav-item nav-link active&quot; href=&quot;index.php&quot;&gt;Home &lt;span class=&quot;sr-only&quot;&gt;(current)&lt;/span&gt;&lt;/a&gt;
        &lt;a class=&quot;nav-item nav-link&quot; href=&quot;form.php&quot;&gt;Order Form&lt;/a&gt;
        &lt;a class=&quot;nav-item nav-link&quot; href=&quot;invoice.php&quot;&gt;Invoice&lt;/a&gt;
          &lt;form action=&quot;login.php&quot; class=&quot;form-inline&quot;&gt;
              &lt;?php echo $loginButton ?&gt;
          &lt;/form&gt;
    
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/nav&gt;
  
  &lt;?php echo $pageContent; ?&gt;
  
  &lt;/div&gt;
    &lt;/body&gt;
  
  &lt;/html&gt;
  </code></pre>
  <p>The config page which I include on the other pages just has session_start(); at the top between the php tags.  The login page has the following code:</p>
  <pre><code>    &lt;?php
      include 'config.php';
      if(!$conn) {
      echo &quot;Failed to connect to the database: &quot; .mysqli_connect_error();
      }
      $pageTitle = &quot;Login Page&quot;;
      $pageContent = NULL;
      $msg = NULL;
      $invalidUser = NULL;
      $invalidPassword = NULL;
  
      if (filter_has_var(INPUT_POST, 'login')) {
          $username = strip_tags(filter_input(INPUT_POST, 'username'));
          $passwordSubmit = trim(filter_input(INPUT_POST, 'password'));
          $valid = TRUE;
      
          if ($username == NULL) {
              $invalidUser = '&lt;span class=&quot;error&quot;&gt;Required Field&lt;/span&gt;';
              $valid = FALSE;
          }
      
          if ($passwordSubmit == NULL) {
              $invalidPassword = '&lt;span class=&quot;error&quot;&gt;Required Field&lt;/span&gt;';
                  $valid = FALSE;
          }
      
      if ($valid) {
          $stmt = $conn-&gt;stmt_init();
          if  ($stmt-&gt;prepare(&quot;SELECT `memberID`, `password` FROM `membership` WHERE `username` = ?&quot;)) {
              $stmt-&gt;bind_param(&quot;s&quot;, $username);
              $stmt-&gt;execute();
              $stmt-&gt;store_result();
              $stmt-&gt;bind_result($memberID, $password);
              $stmt-&gt;fetch();
              $stmt-&gt;free_result();
              $stmt-&gt;close();
          
          } else {
              $msg = &lt;&lt;&lt;HERE
              &lt;h3 class=&quot;error&quot;&gt;We could not find you in the system.  New users must register before gaining access to the site.  If you forgot your login, please use the Password Recover Tool.&lt;/h3&gt;
  HERE;
          }
  
          if (password_verify($passwordSubmit, $password)) {
              $stmt = $conn-&gt;stmt_init();
              if ($stmt-&gt;prepare(&quot;SELECT `firstname`, `lastname`, `email` FROM `membership` WHERE `memberID` = ?&quot;)) {
                  $stmt-&gt;bind_param(&quot;i&quot;, $memberID);
                  $stmt-&gt;execute();
                  $stmt-&gt;store_result();
                  $stmt-&gt;bind_result($firstname, $lastname, $email);
              
                  if ($stmt-&gt;num_rows == 1) {
                      $stmt-&gt;fetch();
                      $_SESSION['memberID'] = $memberID;
                      //$_SESSION['firstname'] = $firstname;
                      //$_SESSION['lastname'] = $lastname;
                      //$_SESSION['email'] = $email;
                      //$_SESSION['loggedIn'] = TRUE;
                  
                      setcookie(&quot;firstname&quot;, $firstname, time()+(3600*3));
                      setcookie(&quot;lastname&quot;, $lastname, time()+(3600*3));
                  
                      $stmt-&gt;close();
                  
                      header(&quot;Location: profile.php&quot;);
                      exit;
                  
                  } else {
                      $msg = &lt;&lt;&lt;HERE
                      &lt;h3 class=&quot;error&quot;&gt;We could access your login records.&lt;/h3&gt;
  HERE;
                  }
              
              } else {
                      $msg = &lt;&lt;&lt;HERE
                      &lt;h3 class=&quot;error&quot;&gt;We could not find your information.&lt;/h3&gt;
  HERE;
              }
          } else {
              $msg = &lt;&lt;&lt;HERE
              &lt;h3 class=&quot;error&quot;&gt;We could not find you in the system.  New users must register before gaining access to the site.  If you forgot your login, please use the Password Recovery Tool.&lt;/h3&gt;
  HERE;
      }
      }
      }
  
      $pageContent .= &lt;&lt;&lt;HERE
      &lt;section class=&quot;container&quot;&gt;
      $msg
      &lt;form action=&quot;login.php&quot; method=&quot;post&quot;&gt;
          &lt;div class=&quot;form-group&quot;&gt;
          &lt;label&gt;Username $invalidUser&lt;/label&gt;
          &lt;input type=&quot;text&quot; class=&quot;form-control&quot; id=&quot;username&quot; name=&quot;username&quot; required /&gt;
          &lt;/div&gt;
          &lt;div class=&quot;form-group&quot;&gt;
          &lt;label&gt;Password $invalidPassword&lt;/label&gt;
          &lt;input type=&quot;password&quot; class=&quot;form-control&quot; id=&quot;password&quot; name=&quot;password&quot; required /&gt;
      
          &lt;/div&gt;
          &lt;input type=&quot;submit&quot; name=&quot;login&quot; value=&quot;Login&quot; class=&quot;btn btn-primary&quot;&gt;
          &lt;/form&gt;
          &lt;/section&gt;
  HERE;
  
      $pageContent .= &quot;&lt;pre&gt;&quot;;
      $pageContent .= &quot;&lt;/pre&gt;&quot;;
      $pageTitle = &quot;Login Page&quot;;
      
      include 'template.php';
      ?&gt;
  </code></pre>