Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

session_start(): Failed to read session data: user (path: ) #14314

Closed
ZhangRuiMingZRM opened this issue Aug 20, 2019 · 4 comments
Assignees
Labels
Projects

Comments

@ZhangRuiMingZRM
Copy link
Contributor

@ZhangRuiMingZRM ZhangRuiMingZRM commented Aug 20, 2019

  • Phalcon version: 4.0.0
  • PHP Version: 7.3.6
  • Operating System: ubuntu
  • Installation type: Compiling from source
  • Server: php web server
use Phalcon\Session\Manager;
use Phalcon\Session\Adapter\Redis;
use Phalcon\Storage\AdapterFactory;

try {
    $session = new Manager();
    $adapterFactory = new AdapterFactory();
    $handler = new Redis(
        $adapterFactory,
        [
            "host" => "127.0.0.1",
            "port" => 6379
        ]
    );
    $session->setHandler($handler);
    $session->start();
} 
...

I got this

PHP Warning:  session_start(): Failed to read session data: user (path: ) in .....

That is because SessionHandlerInterface::read expect string as return value but actually return null

public function read(var id) -> string
{
return this->adapter->get(id);
}

Phalcon\Session\Adapter\Redis::get() may return null

public function get(string! key, var defaultValue = null) -> var
{
return this->getUnserializedData(
this->getAdapter()->get(key),
defaultValue
);
}

This is a solution

in Phalcon/Session/Adapter/AbstractAdapter::read()

    public function read(var id) -> string
    {
      var data;
        let data = this->adapter->get(id);

        return is_null(data) ? "" : data;
    }

And in test unit Phalcon\Test\Integration\Session\Adapter\Redis\ReadCest::sessionAdapterRedisRead()
should add test for another possible situation (if session_id(key) does not exist in redis)

public function sessionAdapterRedisRead(IntegrationTester $I)
{
$I->wantToTest('Session\Adapter\Redis - write()');
$adapter = $this->getSessionRedis();
$value = uniqid();
$I->haveInRedis('string', 'sess-reds-test1', $value);
$expected = $value;
$actual = $adapter->read('test1');
$I->assertEquals($expected, $actual);
$I->sendCommandToRedis('del', 'sess-reds-test1');
}

Test if session_id(key) does not exist in redis

public function sessionAdapterRedisRead(IntegrationTester $I)
{
    .
    .
    . 
   $I->sendCommandToRedis('del', 'sess-reds-test1');
   $I->assertNotNull(
           $adapter->read('test1')
   );
}

So do as Phalcon\Session\Adapter\Libmemcached::get()

@ruudboon ruudboon added this to To do in 4.0 Release via automation Aug 20, 2019
@ruudboon

This comment has been minimized.

Copy link
Member

@ruudboon ruudboon commented Aug 20, 2019

Thank for reporting. Do you have time to open a pull for this? Otherwise I can do this.

@ZhangRuiMingZRM

This comment has been minimized.

Copy link
Contributor Author

@ZhangRuiMingZRM ZhangRuiMingZRM commented Aug 20, 2019

Thank for reporting. Do you have time to open a pull for this? Otherwise I can do this.

Very pleased to do it

@ruudboon

This comment has been minimized.

Copy link
Member

@ruudboon ruudboon commented Aug 20, 2019

Thnx!

This was referenced Aug 20, 2019
@ruudboon ruudboon moved this from To do to In progress in 4.0 Release Aug 20, 2019
@sergeyklay

This comment has been minimized.

Copy link
Member

@sergeyklay sergeyklay commented Aug 20, 2019

Fixed in the 4.0.x branch. Feel free to open a new issue if the problem appears again. Thank you for the bug report.

@sergeyklay sergeyklay closed this Aug 20, 2019
4.0 Release automation moved this from In progress to Done Aug 20, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
4.0 Release
  
Done
3 participants
You can’t perform that action at this time.