Source

erlang_php_session_storage / ErlangSessionHandler.class.php

<?php

class ErlangSessionHandler
{
  protected $host = 'server@127.0.0.1';
  protected $erlang_cookie = 'ABCDEFG';
  protected $conn_timeout = 5;
  
  protected $link = null;
  
  public function open($save_path, $session_name)
  {
    if(null === $this->link)
    {
      $this->link = peb_connect($this->host, $this->erlang_cookie, $this->conn_timeout);
      if(!$this->link)
      {
        throw new Exception(sprintf("Can't connect to the erlang node %s using erlang_cookie %s", $this->host, $this->erlang_cookie));
      }
    }
    return $this->link;
  }
  
  public function close()
  {
    if(is_resource($this->link))
    {
      peb_close($this->link);
    }
  }
  
  public function read($session_id)
  {
    $x = peb_encode("[~s]", array(array($session_id)));
    $result = peb_rpc("session_handler", "read", $x, $this->link);
    $rs = peb_decode($result);
    $data = $rs[0];
    return is_array($data) ? '' : $data;
  }
  
  public function write($session_id, $session_data)
  {
    $x = peb_encode("[~s, ~s]", array(array($session_id, $session_data))); 
    $result = peb_rpc("session_handler", "write", $x, $this->link);
    unset($result);
    return true;
  }
  
  public function destroy($session_id)
  {
    $x = peb_encode("[~s]", array(array($session_id)));
    $result = peb_rpc("session_handler", "destroy", $x, $this->link);
    unset($result);
    return true;
  }
  
  public function gc($max_expirte_time)
  {
    $x = peb_encode('[~i]', array(array($max_expirte_time)));
    $result = peb_rpc("session_handler", "gc", $x, $this->link);
    $rs = peb_decode($result);
    return $rs;
  }
}