Skip to content

Instantly share code, notes, and snippets.

@radityopw
Created April 28, 2022 07:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save radityopw/0a8b9d09ab867a2f152c3c04b90898cd to your computer and use it in GitHub Desktop.
Save radityopw/0a8b9d09ab867a2f152c3c04b90898cd to your computer and use it in GitHub Desktop.
session handler using google cloud storage
<?php
require_once 'vendor/autoload.php';
use Google\Cloud\Storage\StorageClient;
class MySessionHandler implements SessionHandlerInterface{
private $savePath;
private $storage;
private $bucket;
public function __construct(){
$this->savePath = "session-dev";
$this->storage = new StorageClient([
'keyFilePath' => 'session_dev.json'
]);
$this->bucket = $this->storage->bucket($this->savePath);
$this->storage->registerStreamWrapper();
}
public function open($savePath, $sessionName) {
// tidak diimplementasikan karena sudah spesifik ke bucket tertentu di GCP
return true;
}
public function close() {
return true;
}
public function read($id){
return (string) @file_get_contents('gs://'.$this->savePath.'/'.$id);
}
public function write($id, $data){
return file_put_contents('gs://'.$this->savePath.'/'.$id, $data) === false ? false : true;
}
public function destroy($id){
$object = $this->bucket->object($id);
$object->delete();
return true;
}
public function gc($maxlifetime){
// tidak diimplementasikan , lebih baik menggunakan konfigurasi lifecycle di bucket pada gcp
return true;
}
}
$handler = new MySessionHandler();
session_set_save_handler($handler, true);
session_start();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment