Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将php会话作为json存储在redis中,读取会话_Php_Json_Node.js_Session - Fatal编程技术网

将php会话作为json存储在redis中,读取会话

将php会话作为json存储在redis中,读取会话,php,json,node.js,session,Php,Json,Node.js,Session,我将我的php会话作为json字符串存储在redis中,以便与node.js共享它们,就像这里所做的那样: 我不喜欢的是,这些函数将会话存储在磁盘和redis中。我只想把它们放在redis里。作为json 我想改变这个很容易,我想是的,但我不知道怎么做 我无法使用session_encode();关于一个特定的变量。我需要序列化非序列化吗?读取函数必须返回什么格式 public function read($id) { $id = $this->_prefix . $id;

我将我的php会话作为json字符串存储在redis中,以便与node.js共享它们,就像这里所做的那样:

我不喜欢的是,这些函数将会话存储在磁盘和redis中。我只想把它们放在redis里。作为json

我想改变这个很容易,我想是的,但我不知道怎么做

我无法使用session_encode();关于一个特定的变量。我需要序列化非序列化吗?读取函数必须返回什么格式

public function read($id) {

    $id = $this->_prefix . $id;

    try {

        $r = $this->__rc
            ->multi()
            ->get($id)
            ->expire($id, $this->ttl)
            ->exec();

    } catch (\RedisException $e) { return false; }


    $tmp      = $_SESSION;
    $_SESSION = json_decode( $r[0], true );

    if( isset( $_SESSION ) && ! empty( $_SESSION ) && $_SESSION != null ){

        $new_data = session_encode();
        $_SESSION = $tmp;
        return $new_data;

    } 

    else{ 

        return ''; 

    }

}

public function write($id, $data) {

    $tmp = $_SESSION;
    session_decode($data);
    $new_data = $_SESSION;
    $_SESSION = $tmp;

    $id = $this->_prefix . $id;

    try{

        $this->__rc
            ->multi()
            ->set( $id, json_encode($new_data) )
            ->expire($id, $this->ttl)
            ->exec();

    } catch (\RedisException $e) { return false; }

    return true;

}

为了只在Redis中保存会话,而不是在磁盘上保存会话,您需要更改将会话存储在php.ini中的设置,如session_save path