【问题标题】:Extended Session in L5 does not persist between requestsL5 中的扩展会话不会在请求之间持续存在
【发布时间】:2015-03-20 23:41:04
【问题描述】:

我正在扩展会话提供程序以保留一些必需的数据。我开始编辑AppServiceProvider's boot method

\Session::extend('desk', function($app)
{
    return new Desk();
}); 

Desk 类看起来像:

namespace App\Services;


use Illuminate\Session\ExistenceAwareInterface;

class Desk implements \SessionHandlerInterface, ExistenceAwareInterface{

    /**
     * The existence state of the session.

     * @var bool
     */
    protected $exists;

    public function close()
    {
        return true;
    }

    public function destroy($session_id)
    {
        $session = $em->find('Session', $session_id);
        $em->remove($session);
        $em->flush();

        return true;
    }

    public function gc($maxlifetime)
    {
        // TODO: Implement gc() method.
    }

    public function open($save_path, $session_id)
    {
        return true;
    }

    public function read($session_id)
    {
        $session = $em->find('Session', $session_id);

        if ($sesion !== null){
            $this->exists = true;
            return  $session->getPayload();
        }
    }

    public function write($session_id, $session_data)
    {
        $session = $em->find('Session', $session_id);

        if ($session === null){
            $session = new Session($session_id, $session_data);
            $em->persist($session);
        }
        else{
            $session->setPayload($session_data);
        }

        $em->flush();

        $this->exists = true;
    }


    public function setExists($value)
    {
        $this->exists = $value;

        return $this;
    }
}

完成实现后,我将会话配置更改为:

return [
    'driver' => 'desk',
    'lifetime' => 120,
    'expire_on_close' => false,
    'encrypt' => false,
    'files' => storage_path().'/framework/sessions',
    'connection' => null,
    'table' => 'sessions',
    'lottery' => [2, 100],
    'cookie' => 'lote_session',
    'path' => '/',
    'domain' => null,
    'secure' => false,
];

当我加载页面时没有问题,但是在成功登录请求然后刷新页面后,会话过期并且用户再次成为访客。我错过了什么吗?

附加信息:如果我将会话驱动程序恢复为“文件”,一切正常。

【问题讨论】:

    标签: php session laravel laravel-5


    【解决方案1】:

    好吧,对于像我这样需要/想要扩展会话提供程序的其他人,请注意会话的表结构。我的错误是payload 列设置为:

    payload varchar(255) not null
    

    由于 laravel 序列化数据,payload 值可能超过 255 个字符的长度,结果会破坏数据并使其不一致。您可以考虑:

    payload text not null
    

    【讨论】:

      猜你喜欢
      • 2013-09-19
      • 2014-08-28
      • 1970-01-01
      • 1970-01-01
      • 2014-02-12
      • 2011-03-01
      • 2019-04-19
      • 2011-10-29
      相关资源
      最近更新 更多