Permalink
Browse files

session handler example

  • Loading branch information...
1 parent b15fd58 commit 5c4ef5c3d5ff8241c95d734bd6701dc183feb458 @videlalvaro committed May 17, 2010
Showing with 76 additions and 0 deletions.
  1. +52 −0 doc/examples/PHPCaskSessionHandler.class.php
  2. +24 −0 doc/examples/index.php
View
52 doc/examples/PHPCaskSessionHandler.class.php
@@ -0,0 +1,52 @@
+<?php
+
+require_once('./PHPCask.class.php');
+
+class PHPCaskSessionHandler
+{
+ protected $host;
+ protected $erlang_cookie;
+
+ protected $phpcask = null;
+
+ public function __construct($host, $cookie)
+ {
+ $this->host = $host;
+ $this->erlang_cookie = $cookie;
+ }
+
+ public function open($save_path, $session_name)
+ {
+ $this->phpcask = new PHPCask($this->host, $this->erlang_cookie);
+ }
+
+ public function close()
+ {
+ $this->phpcask->close();
+ }
+
+ public function read($session_id)
+ {
+ $data = $this->phpcask->get($session_id);
+ return $data[0] == 'not_found' ? '' : $data[0];
+ }
+
+ public function write($session_id, $session_data)
+ {
+ $result = $this->phpcask->put($session_id, $session_data);
+ return $result[0] == 'ok';
+ }
+
+ public function destroy($session_id)
+ {
+ $result = $this->phpcask->delete($session_id);
+ return $result[0] == 'ok';
+ }
+
+ public function gc($max_expirte_time)
+ {
+ $result = $this->phpcask->merge($max_expirte_time);
+ return $result[0] == 'ok';
+ }
+}
+?>
View
24 doc/examples/index.php
@@ -0,0 +1,24 @@
+<?php
+require_once('./PHPCaskSessionHandler.class.php');
+
+$sh = new PHPCaskSessionHandler('phpcask@localhost', 'OSUCHJLZLTZQBVWFCRAA');
+
+session_set_save_handler(
+ array($sh,"open"),
+ array($sh,"close"),
+ array($sh,"read"),
+ array($sh,"write"),
+ array($sh,"destroy"),
+ array($sh,"gc")
+);
+
+session_start();
+?>
+<?php
+if (!isset($_SESSION['counter'])) {
+ $_SESSION['counter'] = 1;
+} else {
+ $_SESSION['counter']++;
+}
+?>
+Counter: <?php echo $_SESSION['counter']; ?><br />

0 comments on commit 5c4ef5c

Please sign in to comment.