title: Session not recognized in include() page (NOT A DUPLICATE)
body: <p><strong><em>IDK why this guy keeps putting it as duplicate it is not I looked at other examples and they won't fix this problem</em></strong></p>
  
  <p>I have a session that isn't recognised when I use it on a page that is then included in another page like this:</p>
  
  <p><strong>nav.php</strong></p>
  
  <pre><code>include('login.php'); // Has session variables in it
  if(isset($_SESSION['login_user'])){
    echo '&lt;span&gt;Dash&lt;/span&gt;';
  }
  else{
    echo '&lt;span&gt;Login&lt;/span&gt;';
  }
  </code></pre>
  
  <p><strong>index.php</strong></p>
  
  <pre><code>&lt;?php include('nav.php'); ?&gt;
  </code></pre>
  
  <p>I don't know why, but the code above doesn't work. No matter if they are logged in or not, it will still just show 'Login'.</p>
  
  <hr>
  
  <p>I know for sure that the session is started because the code below works:</p>
  
  <p><strong>index.php</strong></p>
  
  <pre><code>include('login.php'); // Has session variables in it
  if(isset($_SESSION['login_user'])){
    echo '&lt;span&gt;Dash&lt;/span&gt;';
  }
  else{
    echo '&lt;span&gt;Login&lt;/span&gt;';
  }
  </code></pre>
  
  <hr>
  
  <p>For some reason when nav.php is included in index.php, it ends up not recognizing the session, but when the code is ran without including any files it works perfectly.</p>
  
  <p><strong>I have looked at the links to show that this is a duplicate and they aren't helping.</strong></p>
  
  <hr>
  
  <p>Edit:</p>
  
  <p>Tried running this code:</p>
  
  <p><strong>login.php</strong></p>
  
  <pre><code>session_set_cookie_params(60 * 30, "/", "domain.com", true); // also tried '.domain.com'
  session_name("login_user");
  session_start();
  </code></pre>
  
  <p><strong>index.php</strong></p>
  
  <pre><code>session_start();
  if(isset($_COOKIE["login_user"])) {
    echo 'Dash';
  }
  else { 
    session_unset(); 
    session_destroy(); 
    session_abort(); 
  }
  </code></pre>
  
  <p>For some reason, the new code doesn't do anything.</p>
  
  <hr>
  
  <p>I'm not sure if this helps, but I tried echoing the $_SESSION variable and this is what happened:</p>
  
  <p><strong>nav.php</strong></p>
  
  <pre><code>include('login.php');
  echo $_SESSION["login_user"];
  </code></pre>
  
  <p><strong>index.php</strong></p>
  
  <pre><code>include('nav.php');
  </code></pre>
  
  <p>The output for this was a notice:</p>
  
  <pre><code>Notice: Undefined index: login_user in nav.php on line 28
  </code></pre>
  
  <p><strong>But</strong> when I tried echoing the code like this:</p>
  
  <p><strong>index.php</strong></p>
  
  <pre><code>include('login.php');
  echo $_SESSION["login_user"];
  </code></pre>
  
  <p>The output was the actual username.</p>
  
  <p>I think for some reason when you use 'include()' to include the code, that looks for a session, it just fails. When you use the code straight up without including another document that has the code, it works perfectly.</p>