psr7-sessions/storageless

unable to use get() method

hatrena opened this issue · 26 comments

When i use $session->set() and $session->get() immediately after it, everything works properly. but after that in the same page (Middleware), when i comment out $session->set(), $session->get() does give me nothing:

$session = $request->getAttribute(\PSR7Session\Http\SessionMiddleware::SESSION_ATTRIBUTE);
//$session->set('user', 'Saeed');
echo $session->get('user');

Did your cookies get transmitted at all? What is the cookie like? Can you try checking its contents?

is there any specific variable that i need to look for in COOKIE? i have PHPSESSID

This lib doesn't use PHPSESSID: it has its own cookie called 'slsession'

The Cookie does not have slsession? any workaround to fix?

Did you try any of the examples first?

yes, i have the same problem

The counter example won't work either? Wondering if you can share how you have set it up...

I am using Zend Expressive and pipe it via SessionMiddleware:

'middleware_pipeline' => [
            'pre_routing' => [
                'middleware' => [
                    ServerUrlMiddleware::class,
                    QueryStringMiddleware::class,
                ],
                'priority' => PHP_INT_MAX,
            ],
            'routing' => [
                'middleware' => [
                    ApplicationFactory::ROUTING_MIDDLEWARE,
                    UrlHelperMiddleware::class,
                    SessionMiddleware::class,
                    ApplicationFactory::DISPATCH_MIDDLEWARE,
                ],
                'priority' => 1,
            ],
        ]

and in `SessionMiddleware i have:
return SessionMiddleware::fromSymmetricKeyDefaults(
'MyKey',
1200
);

Please try out the examples in this project first, and ensure that the
counter example works (you just need to run it, no coding needed)

I have tested the counter, it shows 1 all the time, when i refresh the page

Try:

mkdir tst
cd tst
git clone git@github.com:Ocramius/PSR7Session.git
cd PSR7Session
composer install
php -S localhost:8080 examples/index.php

Now browse to http://localhost:8080 and you should see a counter increment at each page load

I have slsession variable in my cookie, but it returns me true for isEmpty()

@hatrena did the example above work, first of all? I'm trying to understand if this is an environment issue or an integration issue.

The example did not work, but i added those 2 extra libraries in the composer.json and created the SessionMiddleware in the way explained in the example. this way worked!. thanks

The fact that the examples won't work bothers me. Do the unit tests pass?

I have added it to the unit test, assertion process failed since it could not get content from parsed token.

Which test failed?

I ran a middleware test with mocking the request to get the attribute SessionMiddleware::SESSION_ATTRIBUTE

I'm asking about the tests provided with this package :-)

That test passed!

Well I had same problem too. Turned out it won't work while I use it over HTTP on my dev server and not HTTPS. The slsession cookie has secure flag set so it did not get transmitted to the server.

That is intentional: do not use this sort of cookies without TLS.

Yeah it would be nice though to disable this while in dev mode ;)
Anyway maybe @hatrena has same problem?

In the README you said:
Then try accessing http://localhost:9999: you should see a counter that increases at every page refresh

clearly it won't work that way.

See the example dir on how to do that: works without https

thx @Ocramius great work by the way!

Closing since the issue was the "secure" flag (which is mentioned on documentation).