簡體   English   中英

在Redis中將php會話存儲為json,讀取會話

[英]Storing php sessions in redis as json, read the session

我將我的php會話作為json字符串存儲在redis中,以便與node.js共享它們,就像在這里完成的那樣:

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

我不喜歡這些功能將會話存儲在磁盤和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路徑。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM