title: Unable to get session to work for login site php 7.2
body: <p>I'm learning PHP so i'm creating a login site. When the user enters valid credentials the page should be directed to /site.php however it gets re-directed back to index.php when valid creds are entered. I'm doing this with a domain www.example.xyz on port 8080 running on apache2.</p>
  
  <p>I have tested that sessions are working by testing the following in new files:
  <a href="https://www.w3schools.com/php7/php7_sessions.asp" rel="nofollow noreferrer">https://www.w3schools.com/php7/php7_sessions.asp</a></p>
  
  <p>I'm just not sure what i'm doing wrong in my code</p>
  
  <p>I know the connection to the database is good because I've tested the code without any session handling and it works as expected.</p>
  
  <p>index.php</p>
  
  <pre><code>&lt;?php
  
  session_start();
  
  require_once 'classes/membership.php';
  
  $membership = new Membership();
  
  
  
  if(isset($_GET["status"]) &amp;&amp; $_GET["status"] == "loggedout")
  
  {
  
      $membership-&gt;logOut();
  
  }
  
  
  
  if($_POST &amp;&amp; !empty($_POST["username"]) &amp;&amp; !empty($_POST["pwd"]))
  
  {
  
      $response = $membership-&gt;validateUser($_POST["username"], $_POST["pwd"]);
  
  }
  
   ?&gt;
  
  
  
  &lt;!DOCTYPE html&gt;
  
  
  
  &lt;head&gt;
  
  
  
          &lt;title&gt;Login Page&lt;/title&gt;
  
          &lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&gt;
  
          &lt;script src = "http://code.jquery.com/jquery-3.3.1.min.js"&gt;&lt;/script&gt;
  
  
  
          &lt;body&gt;
  
  
  
              &lt;div&gt;
  
  
  
                  &lt;form method="post" action=""&gt;
  
                      &lt;h2&gt;Login &lt;small&gt;enter your credentials&lt;/small&gt;&lt;/h2&gt;
  
  
  
                      &lt;p&gt;
  
                          &lt;label for="name"&gt;Username: &lt;/label&gt;
  
                          &lt;input type="text" name="username" /&gt;
  
                      &lt;/p&gt;
  
  
  
                      &lt;p&gt;
  
                          &lt;label for="pwd"&gt;Password: &lt;/label&gt;
  
                          &lt;input type="password" name="pwd" /&gt;
  
                      &lt;/p&gt;
  
  
  
                      &lt;p&gt;
  
                          &lt;input type="submit" id="submit" value="Login" name="submit" /&gt;
  
                      &lt;/p&gt;
  
  
  
                  &lt;/form&gt;
  
  
  
                  &lt;?php if(isset($response)) echo "&lt;h4&gt;" . $response . "&lt;/h4&gt;"; ?&gt;
  
  
  
              &lt;/div&gt;
  
  
  
          &lt;/body&gt;
  
  
  
  &lt;/head&gt;
  </code></pre>
  
  <p>site.php</p>
  
  <pre><code>&lt;?php
  require_once 'classes/membership.php';
  
  $membership = new Membership();
  
  $membership-&gt;confirmMember();
  
  echo phpinfo();
  
  ?&gt;
  &lt;html&gt;
  &lt;body&gt;
  &lt;p&gt;
    &lt;a href = "index.php?status=loggedout"&gt;Log Out&lt;/a&gt;
  &lt;/p&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>membership.php</p>
  
  <pre><code>&lt;?php
  
  require_once 'mysql.php';
  class Membership
  {
    function validateUser($un,$pw)
    {
      $mysql = new Mysql;
      $validateCreds = $mysql-&gt;verifyUserAndPass($un,md5($pw));
  
      if($validateCreds)
      {
        $_SESSION["status"] == "authorized";
        header("location: /site.php");
      }
  
      else
      {
        return "Enter valid creds.";
      }
    }
  
    function logOut()
    {
      if(isset($_SESSION["status"]))
      {
        unset($_SESSION["status"]);
      }
  
      if(isset($_COOKIE[session_name()]))
      {
        setcookie(session_name(),'',time() - 10000);
      }
  
      session_destroy();
    }
  
    function confirmMember()
    {
      if($_SESSION["status"] != "authorized")
      {
        header("location: /index.php");
      }
    }
  }
   ?&gt;
  </code></pre>
  
  <p>and the Mysql will return true if valid creds are entered.</p>
  
  <p>Expected results are when valid creds are entered the page /site.php should load. Once the user has clicked "log out" on site.php it should not allow them to go to www.example.xyz:8080/site.php and direct them back to index.php</p>
  
  <p>Actual results:
  User enters in valid creds and gets re-directed to index.php. In the network web tools in firefox I see the page /site.php was reached but got a HTTP 302 and then index.php loads again so i'm doing something wrong with the sessions.</p>