Slim 3 Session variables not persistent over routes

823 views Asked by At

As the title reads, I can't get my session variables to be persistent over routes.

I call session_start() at the VERY beginning of my index.php.

Root route:

$app->get('/', function (Request $request, Response $response) {
    $this->logger->addInfo("session id: " . $_SESSION['cus_id']);

    $response = $this->view->render($response, "homepage.phtml", [
        'test' => 'testing something',
        'logged_in' => isset($_SESSION['cus_id'])
    ]);

    return $response;
});

in my app.log I see that the session variable is non existent.

But this is my code after a successful login: (post to /login)

if ($customer !== false && password_verify($password, $customer['password'])) {
        $_SESSION['cus_id'] = $customer['id'];

        return $this->view->render($response, "homepage.phtml", [ 'logged_in' => true]);
    }

I also used the logger here for testing purposes and it showed me that it saved the right id.

I have also used various extra libraries with the exact same result. I also checked the official documentation, to no avail.

Question: How do I get the session variables to be persistent throughout routes? Or is the another way to save sessions? and or another way to solve this login problem?

1

There are 1 answers

0
Aynalem Tesfaye On

I had the same problem after upgrading a server which resulted in a new server setting in the php.ini file. For me, session.auto_start=1 in the php.ini did the trick.