棘轮和本地会议

时间:2018-01-21 15:11:54

标签: php session ratchet

我想在Ratchet中使用原生会话(如果可能的话,不要使用Symfony)。

我跟着并在github上尝试了以下问题/答案:https://github.com/ratchetphp/Ratchet/issues/346

这是onOpen函数的代码:

public function onOpen(ConnectionInterface $conn) {

    $this->clients->attach($conn);

    echo "New Connection\n";

    $cookiesRaw = $conn->httpRequest->getHeader('Cookie');

    if(count($cookiesRaw)) {
        $cookiesArr = \GuzzleHttp\Psr7\parse_header($cookiesRaw)[0]; // Array of cookies
    }


    $conn->session = new \session($cookiesArr['PHPSESSID']);

    $conn->session->set('username','John');

    echo $conn->session->get('username');
}

这是我的会话课程:

class session {

  // the id of the client session;
  public $id;

  // take a session Id and continue a session if empty create a new session
  public function __construct($PHPSESSID = ''){

    // if we have a valid session id containing letters and numbers then use it
    // @note add check for weird characters later
    if (strlen($PHPSESSID) > 5 && preg_match('/[A-Za-z]/', $PHPSESSID) && preg_match('/[0-9]/', $PHPSESSID)){
      session_id($PHPSESSID);
    } else{
      session_regenerate_id();
    }

    @session_start();
    $this->id = session_id();

    unset($_SESSION); // so we cant attempt to use this directly
    session_write_close();
  }

  // return the value of a session variable or null
  public function get($var_name){
    session_id($this->id);
    @session_start();

    if(isset($_SESSION[$var_name])){
      $var =  $_SESSION[$var_name];

    } else{
      $var = NULL;
    }

    unset($_SESSION);
    session_write_close();

    return $var;
  }

  // set the value of a session variable
  public function set($var_name, $var_value){
    session_id($this->id);
    @session_start();

    $_SESSION[$var_name] = $var_value;

    unset($_SESSION);
    session_write_close();
  }

}

这不起作用。这是来自onOpen函数的var_dump:

object(session)#69 (1) {
  ["id"]=>
  string(64) "17b755a58065c76d34f3476c686cc9a6abb77fd9aa5bae084dd24871bd3a5500"
}

从var_dump可以看出,对象中没有会话。

有没有人有想法?

提前致谢!

0 个答案:

没有答案