概述 快速入门 教程 手册 最佳实践 组件 参考 贡献

发布于 2015-08-27 16:44:26 | 143 次阅读 | 评论: 0 | 来源: 网络整理

The session proxy mechanism has a variety of uses and this example demonstrates two common uses. Rather than injecting the session handler as normal, a handler is injected into the proxy and registered with the session storage driver:

use SymfonyComponentHttpFoundationSessionSession;
use SymfonyComponentHttpFoundationSessionStorageNativeSessionStorage;
use SymfonyComponentHttpFoundationSessionStorageHandlerPdoSessionHandler;

$proxy = new YourProxy(new PdoSessionHandler());
$session = new Session(new NativeSessionStorage(array(), $proxy));

Below, you’ll learn two real examples that can be used for YourProxy: encryption of session data and readonly guest sessions.

Encryption of Session Data

If you wanted to encrypt the session data, you could use the proxy to encrypt and decrypt the session as required:

use SymfonyComponentHttpFoundationSessionStorageProxySessionHandlerProxy;

class EncryptedSessionProxy extends SessionHandlerProxy
{
    private $key;

    public function __construct(SessionHandlerInterface $handler, $key)
    {
        $this->key = $key;

        parent::__construct($handler);
    }

    public function read($id)
    {
        $data = parent::read($id);

        return mcrypt_decrypt(MCRYPT_3DES, $this->key, $data);
    }

    public function write($id, $data)
    {
        $data = mcrypt_encrypt(MCRYPT_3DES, $this->key, $data);

        return parent::write($id, $data);
    }
}

Readonly Guest Sessions

There are some applications where a session is required for guest users, but where there is no particular need to persist the session. In this case you can intercept the session before it is written:

use FooUser;
use SymfonyComponentHttpFoundationSessionStorageProxySessionHandlerProxy;

class ReadOnlyGuestSessionProxy extends SessionHandlerProxy
{
    private $user;

    public function __construct(SessionHandlerInterface $handler, User $user)
    {
        $this->user = $user;

        parent::__construct($handler);
    }

    public function write($id, $data)
    {
        if ($this->user->isGuest()) {
            return;
        }

        return parent::write($id, $data);
    }
}
最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务