title: PHP Session not working on the server
body: <p>I'm making a super simple ticket tracking system with PHP and sessions and I'm having some trouble, I have the following pages:</p>
  
  <ul>
  <li>admin.php (the page where you can create, edit and delete tickets)</li>
  <li>login.php (the page where you login)</li>
  <li>logout.php (not a page, just the logout code)</li>
  <li>edit.php</li>
  <li>add.php</li>
  <li>delete.php</li>
  </ul>
  
  <p>When you go to the admin.php if the session isn't set, it takes you to login.php.</p>
  
  <p>In login.php if the session is set, it takes you to admin.php.</p>
  
  <p>The username and password is stored in the login.php file as plaintext because secutiry isn't the main concern here and it's just a username and a password.</p>
  
  <p>This is the code in admin.php</p>
  
  <pre><code>&lt;?php
      include "includes/header.php";
      include "includes/config.php";
      include "includes/db.php";
  
      session_start();
      $admin = isset($_SESSION['myAdmin']) ? $_SESSION['myAdmin'] : false;
  
      if(!$admin){
          header('Location: login.php');
      }
  
      $db = connect();
  
      $stmt = $db-&gt;query('SELECT * FROM tickets');
      $stmt-&gt;execute();
  
      $result = $stmt-&gt;fetchAll(PDO::FETCH_ASSOC);
  ?&gt;
  </code></pre>
  
  <p>This is the code in login.php</p>
  
  <pre><code>&lt;?php
      include "includes/header.php";
  
      session_start();
      $admin = isset($_SESSION['myAdmin']) ? $_SESSION['myAdmin'] : false;
  
      if(isset($_SESSION['myAdmin'])){
          header('Location: admin.php');
      }
  
      $user = 'myUsername';
      $pass = 'myPassword';
  
      $username = isset($_POST['username']) ? $_POST['username'] : false;
      $password = isset($_POST['password']) ? $_POST['password'] : false;
  
      if($username &amp;&amp; $password){
          if($user == $username &amp;&amp; $pass == $password){
              $_SESSION['myAdmin'] = true;
              header('Location: admin.php');
          }
      }
  ?&gt;
  </code></pre>
  
  <p>It's working wonders on localhost, but I can't get it to work on my server. Both are running PHP 7.0 and all the database configs are fine.</p>
  
  <p>This is what happens:</p>
  
  <p>When I log in and put the right username and password it just reloads login.php, now if I go to admin.php it shows the admin page as if the session was created. </p>
  
  <p>Even if I delete all browsing data or try it in incognito mode. It still does the same. Is there something I'm not seeing? Thanks in advance.</p>
  
  <p>By the way, this is the logout code:</p>
  
  <pre><code>&lt;?php
      session_start();
      unset($_SESSION['myAdmin']);
      header('Location: login.php');
  ?&gt;
  </code></pre>
  
  <p>When I open admin.php I can see the logout link and when I click it takes me login.php but I can still go to admin.php if I type it in the browser.</p>
  
  <p>What's throwing me off is that I don't know how to debug, because when the variable is null or undefined or false/true, echo won't show anything.</p>
  
  <p>EDIT: Ok so, after each <code>header</code> line I've added this <code>exit();</code> and the page shows blank after login or in admin by default. And now I can see the problem clear.</p>
  
  <p>The header (redirect) is not working. When using ini_set('display_errors', 'On'); the error says:</p>
  
  <p>Warning: Cannot modify header information - headers already sent by and it points the line with the <code>header</code>. I suspect the session_start() may have something to do with it.</p>