slimphp / Slim

Slim is a PHP micro framework that helps you quickly write simple yet powerful web applications and APIs.
http://slimframework.com
MIT License
11.98k stars 1.95k forks source link

Session doesn't persist in different requests using \Slim\Middleware\SessionCookie #927

Closed fredw closed 9 years ago

fredw commented 10 years ago

I'm using middleware \Slim\Middleware\SessionCookie to work with sessions, but the values are not persisting in different requests.

I created a small test to simulate the problem:

File 1:

require __DIR__ . '/vendor/autoload.php';

$app = new \Slim\Slim;
$app->add(new \Slim\Middleware\SessionCookie);

$_SESSION['teste'] = 'xxx';

File 2:

require __DIR__ . '/vendor/autoload.php';

$app = new \Slim\Slim;
$app->add(new \Slim\Middleware\SessionCookie);

var_dump($_SESSION['teste']);

First I make a request to File 1, after to File 2. The result is "null". I'm doing something wrong?

tuupola commented 10 years ago

You must do $app->run() in the end of your code. Following works for me.

require_once "../vendor/autoload.php";

$app = new \Slim\Slim();
$app->add(new \Slim\Middleware\SessionCookie());

$app->get("/foo", function() {
    $_SESSION["test"] = "foo";
});

$app->get("/bar", function() {
    var_dump($_SESSION);
});

$app->run();
shanehou commented 9 years ago

When you add that middleware, you should no long use $_SESSION. Instead, you may use encrypted cookies. From source code, you can see all functions about session are assigned to some functions that return boolean or empty string.

silentworks commented 9 years ago

@fredw I am closing this one out, if you are still having trouble please open up a forum topic over at http://help.slimframework.com/ or come see us on irc channel #slimphp on freenode.

w3guy commented 9 years ago

I am having trouble persisting session.

Am using HybridAuth library in a project that requires session_start to work.

Including $app->add(new \Slim\Middleware\SessionCookie()); make things go awry but removing it return things back to normal.

Can someone please give a break down on how session can be persisted in different routes in slim framework?

w3guy commented 9 years ago

Figured it out...

ejrudy commented 8 years ago

How did you solve it @collizo4sky ?

w3guy commented 8 years ago

Since hybridAuth came with its own session handler, I had to use that instead of using that of slim.

NazgulTuga commented 5 years ago

I know this is old, but i was having a similar problem with sessions variables that were being "destroyed" when entering middlewares, refreshing the page or changing pages.

The only thing that worked for me was setting the session lifetime to higher than 0. For some reason my session maxlifetime was 0...

if (!isset($_SESSION)) session_start(); ini_set('session.cookie_lifetime', 3600); ini_set('session.gc_maxlifetime', 3600); session_set_cookie_params('3600'); setcookie(session_name(),session_id(),time()+3600);

Changing the urls in my html from "http://www" to "https://www" also helped...

saheedoladele commented 2 years ago

I hope this is still open, i am having a serious issue using session wilt slim, my session is not accessible outside the route it was defined.

w3guy commented 2 years ago

@saheedoladele i recently used this lib for a project https://github.com/bryanjhv/slim-session

Worked as expected