我怎样才能在我的类上扩展 sessionHandler 并使用 read、write,...在我的班级里?

How can i extends sessionHandler on my class and use read, write,... in my class?

提问人:Mostafa Taha 提问时间:10/2/2023 最后编辑:Mostafa Taha 更新时间:10/7/2023 访问量:26

问:

我正在尝试使用 sessionHandle r 来编写和读取会话,以便我可以使用 openssl_encrypt() 和 openssl_decrypt(),但是当我在我的类中使用公共会话 read() 时,它会在方法下的 vs 红线中显示我,如果我使用 sessionHandler类中存在的任何方法,它表明我有问题......那么我该如何扩展sessionHandler clase并使用其方法呢?

class SessionManager extends SessionHandler
{
    private $sessionName = 'MYAPPSESSION';
    private $sessionMaxLifetime = 0;        
    private $sessionSSL = false;
    private $sessionHTTPOnly = true;        
    private $sessionPath = "/";
    private $sessionDomain = ".mvcapp2.test";   

    private $sessionSavePath = SESSION_SAVE_PATH;

    private $sessionCipherAlgo = "AES-128-ECB";
    private $sessionCipherKey = "WQAS201VXZP@221D";


    public function __construct()
    {
        ini_set('session.use_cookies', 1);
        ini_set('session.use_only_cookies', 1);
        ini_set('session.use_trans_sid', 0);
        ini_set('session.save_handler', "files");

        session_name($this->sessionName); 

        session_save_path($this->sessionSavePath);
        session_set_cookie_params(
            $this->sessionMaxLifetime,
            $this->sessionPath,
            $this->sessionDomain,
            $this->sessionSSL,
            $this->sessionHTTPOnly
        );

        session_set_save_handler($this, true);
    }

    public function read($id)
    {
        return openssl_decrypt(parent::read($id), $this->sessionCipherAlgo, $this->sessionCipherKey);
    }
    public function write($id, $data)
    {
        return parent::write($id, openssl_encrypt($data, $this->sessionCipherAlgo, $this->sessionCipherKey));
    }
    public function start()
    {
        if ("" === session_id()) {
            return session_start();
        }
    }
}
php oop 会话 cookie 扩展

评论


答:

0赞 الرحمن الرحیم 10/7/2023 #1

请尝试以下操作:

class SessionManager extends SessionHandler
{
    // ...

    public function read($id)
    {
        return openssl_decrypt(parent::read($id), $this->sessionCipherAlgo, $this->sessionCipherKey);
    }

    public function write($id, $data)
    {
        return parent::write($id, openssl_encrypt($data, $this->sessionCipherAlgo, $this->sessionCipherKey));
    }

    // ...
}

/**
 * @method string read(string $id)
 * @method bool write(string $id, string $data)
 */
class SessionManager extends SessionHandler
{
    // ...

    public function read($id)
    {
        return openssl_decrypt(parent::read($id), $this->sessionCipherAlgo, $this->sessionCipherKey);
    }

    public function write($id, $data)
    {
        return parent::write($id, openssl_encrypt($data, $this->sessionCipherAlgo, $this->sessionCipherKey));
    }

    // ...
}