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.