1

IDK why this guy keeps putting it as duplicate it is not I looked at other examples and they won't fix this problem

I have a session that isn't recognised when I use it on a page that is then included in another page like this:

nav.php

include('login.php'); // Has session variables in it
if(isset($_SESSION['login_user'])){
  echo '<span>Dash</span>';
}
else{
  echo '<span>Login</span>';
}

index.php

<?php include('nav.php'); ?>

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'.


I know for sure that the session is started because the code below works:

index.php

include('login.php'); // Has session variables in it
if(isset($_SESSION['login_user'])){
  echo '<span>Dash</span>';
}
else{
  echo '<span>Login</span>';
}

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.

I have looked at the links to show that this is a duplicate and they aren't helping.


Edit:

Tried running this code:

login.php

session_set_cookie_params(60 * 30, "/", "domain.com", true); // also tried '.domain.com'
session_name("login_user");
session_start();

index.php

session_start();
if(isset($_COOKIE["login_user"])) {
  echo 'Dash';
}
else { 
  session_unset(); 
  session_destroy(); 
  session_abort(); 
}

For some reason, the new code doesn't do anything.


I'm not sure if this helps, but I tried echoing the $_SESSION variable and this is what happened:

nav.php

include('login.php');
echo $_SESSION["login_user"];

index.php

include('nav.php');

The output for this was a notice:

Notice: Undefined index: login_user in nav.php on line 28

But when I tried echoing the code like this:

index.php

include('login.php');
echo $_SESSION["login_user"];

The output was the actual username.

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.

dobson
  • 461
  • 6
  • 14
  • Try to use Sessioncookie https://www.php.net/manual/en/function.session-set-cookie-params.php – Andreas Schmidt Oct 06 '19 at 22:20
  • I can see the PHPSESSID & __cfduid cookies in the browser if that helps.. What params should I enforce? – dobson Oct 06 '19 at 22:32
  • I‘m currentlly on mobile. Will provide some Ideas tommorow. – Andreas Schmidt Oct 06 '19 at 22:35
  • Where do you use session_start() – SpacePhoenix Oct 07 '19 at 00:01
  • It's inside login.php, which is included at the top. – dobson Oct 07 '19 at 00:29
  • Try this: `session_set_cookie_params(60 * 30, "/", "yourdomain", true); session_name("yourSessionName"); session_start();` See the Docs [here](https://www.php.net/manual/en/function.session-set-cookie-params.php). Then you can check the existens of your Cookie: `if(isset($_COOKIE["yourSessionName"])) { // Cookie is OK } else { session_unset(); session_destroy(); session_abort(); }` – Andreas Schmidt Oct 07 '19 at 08:50
  • I can see the cookies it is making, but for some reason, the code doesn't do anything.. Even when I put it in the index, without including it. I'm going to update the main post with the code I used. – dobson Oct 07 '19 at 16:39
  • I also added another section because I tried echoing the session. – dobson Oct 07 '19 at 16:58

0 Answers0