简体   繁体   中英

Storing php sessions in redis as json, read the session

I'm storing my php sessions in redis as json string to share them with node.js like it is done here:

http://simplapi.wordpress.com/2012/04/13/php-and-node-js-session-share-redi/

What I don't like is that these functions store the sessions on disk and in redis. I just want them in redis. As json.

I thought it would be easy to change this and I guess it is but I don't know how.

I can't use session_encode(); on a specific variable. Do i need to serialize unserialze? What format do I have to return for the read function?

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路径。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM