Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * memcacheSessionHandler class
- * @class memcacheSessionHandler
- * @file memcacheSessionHandler.class.php
- * @brief This class is used to store session data with memcache, it store in json the session to be used more easily in Node.JS
- * @version 0.1
- * @date 2012-04-11
- * @author Deisss
- * @licence LGPLv3
- * This class is used to store session data with memcache, it store in json the session to be used more easily in Node.JS
- */
- class memcacheSessionHandler implements \SessionHandlerInterface {
- private $host = "127.0.0.1";
- private $port = 11211;
- private $lifetime = 0;
- private $memcache = null;
- /**
- * Constructor
- */
- public function __construct(){
- $this->memcache = new Memcache;
- $this->memcache->connect($this->host, $this->port) or die("Error : Memcache is not ready");
- //session_set_save_handler(&$this, true);
- session_set_save_handler(
- array($this, "open"),
- array($this, "close"),
- array($this, "read"),
- array($this, "write"),
- array($this, "destroy"),
- array($this, "gc")
- );
- }
- /**
- * Destructor
- */
- public function __destruct(){
- session_write_close();
- $this->memcache->close();
- }
- /**
- * Open the session handler, set the lifetime ot session.gc_maxlifetime
- * @return boolean True if everything succeed
- */
- public function open($save_path, $session_name){
- $this->lifetime = ini_get('session.gc_maxlifetime');
- return true;
- }
- /**
- * Read the id
- * @param string $id The SESSID to search for
- * @return string The session saved previously
- */
- public function read($id){
- $tmp = $_SESSION;
- $_SESSION = json_decode($this->memcache->get("sessions/{$id}"), true);
- if(isset($_SESSION) && !empty($_SESSION) && $_SESSION != null){
- $new_data = session_encode();
- $_SESSION = $tmp;
- return $new_data;
- }else{
- return "";
- }
- }
- /**
- * Write the session data, convert to json before storing
- * @param string $id The SESSID to save
- * @param string $data The data to store, already serialized by PHP
- * @return boolean True if memcached was able to write the session data
- */
- public function write($id, $data){
- $tmp = $_SESSION;
- session_decode($data);
- $new_data = $_SESSION;
- $_SESSION = $tmp;
- return $this->memcache->set("sessions/{$id}", json_encode($new_data), 0, $this->lifetime);
- }
- /**
- * Delete object in session
- * @param string $id The SESSID to delete
- * @return boolean True if memcached was able delete session data
- */
- public function destroy($id){
- return $this->memcache->delete("sessions/{$id}");
- }
- /**
- * Close gc
- * @return boolean Always true
- */
- public function gc($maxlifetime){
- return true;
- }
- /**
- * Close session
- * @return boolean Always true
- */
- public function close(){
- return true;
- }
- }
- new memcacheSessionHandler();
- ?>
- /* above is memcacheSessionHandler.class.php and next is your autoload.php, just load the memcacheSessionHandler.class.php file then do */
- $handler = new memcacheSessionHandler();
- session_set_save_handler($handler, true);
- /* finally in php.ini I have the following */
- extension=php_memcache.dll
- [Memcache]
- memcache.allow_failover = 1
- memcache.max_failover_attempts=20
- memcache.chunk_size =8192
- memcache.default_port = 11211
- /* so with memcached server/dameon running you'll now store your sessions to it instead of the default file. test.example
- <?php
- include_once(dirname(__FILE__).'/classes/autoload.php');
- session_start();
- $_SESSION['ID'] = 3;
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement