PHP not recognizing SESSION when the code is included

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.

There’s probably something in/about the index.php page that is causing the session_start() in it to fail. Do you have php’s error_reporting set to E_ALL and display_errors set to ON, preferably in the php.ini on your system, so that php will report and display all errors?

Yes, both of those are set to what you said. I’m not seeing any errors though.

The index.php is the only page that is starting the session aswell.

Just tried this at start of index.php:

if (!isset($_SESSION)){
  session_start();
}
else{
  echo 'session already started';
}

It didn’t echo session already started, so I believe errors are working fine and this is the only session_start being ran.

I also copied the code from nav.php and just put it in the index.php file and it could see the session.

Check the cookies in the browser tools. You should see a PHPSID if the session is running

1 Like

Are you sure? What does a phpinfo(); statement show for those settings?

Also, any chance you are altering the code when you are posting it here, such as removing the leading part of a url because you don’t want to show a domain name in the post? This symptom can be caused by using a URL in the include statement, rather than a file system path.

Some more possibilities -

  1. You are redirecting all over your site and what you are seeing is the result of a second request to the page, where the session id cookie doesn’t match where it was originally set at.
  2. You have some code that’s clearing the session variable, that’s located after a redirect which is missing an exit/die statement to stop program execution.

If you cannot determine what’s causing the problem, you will need to post all the code for the relevant files and pages, for the login system and the index/nav files. If there’s anything in the code that you want to redact, xxxxx it out so that we know where it was, rather than completely removing it.

1 Like

When I include files, I am using a system path and not a url.

I’ll try to check this now.

I do see the session in the cookies place.

Yeah, should have looked close before I replied, I am using system paths everywhere EXCEPT the nav.

Thanks for your replies!

Sponsor our Newsletter | Privacy Policy | Terms of Service