php 我如何在我的类上扩展会话并使用read,write,.在我的班上吗

b09cbbtk  于 2023-10-15  发布在  PHP
关注(0)|答案(1)|浏览(109)

我试图使用sessionHandler来写和读会话,所以我可以使用openssl_encrypt()openssl_decrypt(),但当我在我的类中使用公共会话read()时,它会在方法下的vs红线中显示我,如果我使用sessionHandle类中存在的任何方法,它会显示我有什么问题。那么我如何扩展会话类并使用它的方法呢?

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();
        }
    }
}
ssgvzors

ssgvzors1#

尝试如下:

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));
    }

    // ...
}

相关问题