Hello Couchbase community,
I’d like to share my experience in setting up Couchbase as a session handler in PHP and using igbinary for efficient encoding and decoding. I’ve encountered a few challenges and would appreciate your insights and solutions.
I’ve been working on configuring Couchbase as a session handler for PHP applications. Here’s an overview of the steps I’ve taken:
- Couchbase Integration: I’ve successfully integrated Couchbase into my PHP environment using the official Couchbase SDK.
- Custom Session Handler: I’ve created a custom session handler class,
CouchbaseSessionHandler
, which implements theSessionHandlerInterface
. This class manages session-related operations. - PHP Configuration: I’ve configured the PHP
session.save_handler
to use “memcached”, set thesession.save_path
to specify the Couchbase server’s address and port (e.g., “couchbase://localhost:11211”). - In my custom session handler, I handle session initiation, termination, read, write, and session data destruction.
All the code works as in supposed to but when i use and set the session.serialize_handler=igbinary i also need to set the couchbase transcoder to accept igbinary and here comes the problem:
I try like this:
$bucket->setTranscoder('igbinary_serialize', 'igbinary_unserialize');
“Call to a member function get() on bool” Error
<?php
require_once '/path_to/autoload.php';
class myCouchbase {
var $classname = "myCouchbase";
private $cluster = null;
private $Cas = null;
private \Couchbase\Exception\CouchbaseException $Exception;
public function __construct($ConnectionString = "", $user = "", $password = "") {
if ($ConnectionString && $user && $password) {
$options = new \Couchbase\ClusterOptions();
$options->credentials($user, $password);
$cluster = new \Couchbase\Cluster($ConnectionString, $options);
$this->cluster = $cluster;
}
}
public function getCollection($bucket_label, $scope_label, $collection_label){
try {
if ($this->cluster) {
$bucket = $this->cluster->bucket($bucket_label);
$bucket->setTranscoder('igbinary_serialize', 'igbinary_unserialize');
if($bucket) $scope = $bucket->scope($scope_label);
if($scope) $collection = $scope->collection($collection_label);
return $collection;
}
}
catch (\Couchbase\Exception\CouchbaseException $ex) {
$this->Exception = $ex;
return false;
}
}
public function getDocument($bucket_label, $scope_label, $collection_label, $key) {
$collection = $this->getCollection($bucket_label, $scope_label, $collection_label, $key);
try {
$result = $collection->get($key);
$this->Cas = $result->cas();
return $result->content();
}
catch (\Couchbase\Exception\DocumentNotFoundException $ex) {
$this->Cas = null;
return false;
}
catch (\Couchbase\Exception\CouchbaseException $ex) {
$this->Exception = $ex;
return false;
}
}
public function erase($bucket_label, $scope_label, $collection_label, $key) {
$collection = $this->getCollection($bucket_label, $scope_label, $collection_label, $key);
try {
$collection->remove($key);
return true;
}
catch (\Couchbase\Exception\CouchbaseException $ex) {
if ($ex->getCode() == "COUCHBASE_KEY_ENOENT") {
$this->Cas = null;
return true;
} else {
$this->Exception = $ex;
return false;
}
}
}
public function insert($bucket_label, $scope_label, $collection_label, $key, $value, $expiry = 0) {
$collection = $this->getCollection($bucket_label, $scope_label, $collection_label, $key);
try {
$options = null;
if ($expiry || $this->Cas) {
$options = new \Couchbase\InsertOptions();
if ($expiry) $options->expiry($expiry);
}
$result = $collection->insert($key, $value, $options ? $options : null);
$this->Cas = $result->cas();
return true;
}
catch (\Couchbase\Exception\CouchbaseException $ex) {
$this->Exception = $ex;
return false;
}
}
public function upsert($bucket_label, $scope_label, $collection_label, $key, $value, $expiry = 0) {
$collection = $this->getCollection($bucket_label, $scope_label, $collection_label, $key);
try {
$options = null;
if ($expiry || $this->Cas) {
$options = new \Couchbase\UpsertOptions();
if ($expiry) $options->expiry($expiry);
}
$result = $collection->upsert($key, $value, $options ? $options : null);
return $result->cas();
}
catch (\Couchbase\Exception\CouchbaseException $ex) {
$this->Exception = $ex;
return false;
}
}
public function get_cluster(){
return $this->cluster;
}
}
?>
The bizarre thing is that when I don’t use igbinary and serialize the session with the default PHP serialization, everything works as expected.
Note:
I forgot to say if I didn’t have this line:
$bucket->setTranscoder('igbinary_serialize', 'igbinary_unserialize');
Fatal error: Uncaught JsonException: Malformed UTF-8 characters, possibly incorrectly encoded in Couchbase/JsonTranscoder.php on line 77JsonException: Malformed UTF-8 characters, possibly incorrectly encoded in Couchbase/JsonTranscoder.php on line 77