$site->user() returns false on frontend code but seems to evaluate true within controllers

Hello,

I’ve followed everything laid out in the guide about adding non-panel users to to my site. But now, no matter what I do, a var_dump($site->user()) in the header at the beginning of the <body> tag always returns false.

I’m especially confused because the logic at the beginning of the login controller redirecting already logged-in users to the home page does work:

When i start from a fresh session and go to mysite.biz/login the form appears. I fill it out and submit it, then get directed back to the home directory, just as it specifies in that same file:

if($user = $site->user(get('username')) and $user->login(get('password'))) {
  // redirect to the homepage
  // if the login was successful
  go('/');
...

now, if i try to go to mysite.biz/login again i am redirected to mysite.biz/ as per:

return function($site, $pages, $page) {
  if($site->user()) go('/');
...

during all of this, no matter what happens, the var_dump($site->user()); continually returns false. That means I can be staring at the home page, read the bool(false) referring to if someone is logged in, go to mysite.biz/login, and then be redirected away from the page because it says i am logged in, and it will still be false.

I don’t know how to solve this or what’s happening. It seems like the site/controllers/login.php has access to whether someone is logged in but anything in site/snippets or site/templates does not.

Any advice on how to fix this?

ETA: I should mention the role of the user i am testing this for is Client, just as it’s been specified in that same tutorial. Whether I’m logged in as my own Admin account in the panel also appears to have no bearing on the $site->user() returning false

ETA 2: I just made a controller for the homepage (site/controllers/home.php) that literally just returns $site->user() and var_dump() is now acting as expected:

return function($site, $pages, $page) {
  return($site->user());
};

Which i suppose is a useable workaround but obviously something isn’t working as expected here, can anyone help me figure out why/how to fix it?

I can’t reproduce this. Do you have caching enabled?

I downloaded a starterkit and copied files over one by one, and found that the issue came from the header file itself. I was establishing a session with

if(!isset($_SESSION)){ session_start() }

because I didn’t realize I should instead be using

s::start()

I don’t know the intricacies of why that caused the issue but swapping them seems to have solved the problem.

Looks like I need to take another, closer, read through the Toolkit 101… ha.