I have some code that requires checking is a session is there. This code is on a navbar, which I am including in the index.php like this:
index.php
include('nav.php');
The problem is that whenever the nav.php is included in the index.php it can’t recognise the session, however, when you go directly to the nav.php page, it can recognize the session and run the code perfectly.
Code examples:
First Example (where it doesn’t work)
index.php
session_start();
include('nav.php');
nav.php
if(isset($_SESSION['login_user'])){
echo '<span>Dash</span>';
}
else{
echo '<span>Login</span>';
}
The output for this code is just ‘Login’ because for some reason it can’t see the session.
Second Example (where it works)
nav.php
session_start();
if(isset($_SESSION['login_user'])){
echo '<span>Dash</span>';
}
else{
echo '<span>Login</span>';
}
The output for this is ‘Dash’ because it can see the session.
The problem
In the second example, I don’t bother to include the nav… I just go straight to the nav.php page.
For some reason, when I include the nav.php page, it doesn’t work, but when I go straight to the nav.php page it does.