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
Closed

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

ZhangRuiMingZRM opened this issue Aug 20, 2019 · 4 comments
Assignees
Labels
bug A bug report status: low Low

Comments

@ZhangRuiMingZRM
Copy link
Contributor

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
Copy link
Member

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

@ZhangRuiMingZRM
Copy link
Contributor Author

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

Very pleased to do it

@ruudboon
Copy link
Member

Thnx!

This was referenced Aug 20, 2019
@sergeyklay
Copy link
Contributor

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.

@niden niden added bug A bug report status: low Low and removed Bug - Low labels Dec 23, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A bug report status: low Low
Projects
None yet
Development

No branches or pull requests

4 participants