Advertisement
Guest User

Untitled

a guest
Jan 29th, 2015
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.00 KB | None | 0 0
  1. <?php
  2. /**
  3.  * memcacheSessionHandler class
  4.  * @class                       memcacheSessionHandler
  5.  * @file                        memcacheSessionHandler.class.php
  6.  * @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
  7.  * @version                     0.1
  8.  * @date                        2012-04-11
  9.  * @author                      Deisss
  10.  * @licence                     LGPLv3
  11.  * This class is used to store session data with memcache, it store in json the session to be used more easily in Node.JS
  12.  */
  13. class memcacheSessionHandler implements \SessionHandlerInterface {
  14.     private $host = "127.0.0.1";
  15.     private $port = 11211;
  16.     private $lifetime = 0;
  17.     private $memcache = null;
  18.  
  19.     /**
  20.      * Constructor
  21.      */
  22.     public function __construct(){
  23.         $this->memcache = new Memcache;
  24.         $this->memcache->connect($this->host, $this->port) or die("Error : Memcache is not ready");
  25.        
  26.         //session_set_save_handler(&$this, true);
  27.        
  28.         session_set_save_handler(
  29.             array($this, "open"),
  30.             array($this, "close"),
  31.             array($this, "read"),
  32.             array($this, "write"),
  33.             array($this, "destroy"),
  34.             array($this, "gc")
  35.         );
  36.        
  37.     }
  38.  
  39.     /**
  40.      * Destructor
  41.      */
  42.     public function __destruct(){
  43.         session_write_close();
  44.         $this->memcache->close();
  45.     }
  46.  
  47.     /**
  48.      * Open the session handler, set the lifetime ot session.gc_maxlifetime
  49.      * @return boolean True if everything succeed
  50.      */
  51.     public function open($save_path, $session_name){
  52.         $this->lifetime = ini_get('session.gc_maxlifetime');
  53.         return true;
  54.     }
  55.  
  56.     /**
  57.      * Read the id
  58.      * @param string $id The SESSID to search for
  59.      * @return string The session saved previously
  60.      */
  61.     public function read($id){
  62.         $tmp = $_SESSION;
  63.         $_SESSION = json_decode($this->memcache->get("sessions/{$id}"), true);
  64.         if(isset($_SESSION) && !empty($_SESSION) && $_SESSION != null){
  65.             $new_data = session_encode();
  66.             $_SESSION = $tmp;
  67.             return $new_data;
  68.         }else{
  69.             return "";
  70.         }
  71.     }
  72.  
  73.     /**
  74.      * Write the session data, convert to json before storing
  75.      * @param string $id The SESSID to save
  76.      * @param string $data The data to store, already serialized by PHP
  77.      * @return boolean True if memcached was able to write the session data
  78.      */
  79.     public function write($id, $data){
  80.         $tmp = $_SESSION;
  81.         session_decode($data);
  82.         $new_data = $_SESSION;
  83.         $_SESSION = $tmp;
  84.         return $this->memcache->set("sessions/{$id}", json_encode($new_data), 0, $this->lifetime);
  85.     }
  86.  
  87.     /**
  88.      * Delete object in session
  89.      * @param string $id The SESSID to delete
  90.      * @return boolean True if memcached was able delete session data
  91.      */
  92.     public function destroy($id){
  93.         return $this->memcache->delete("sessions/{$id}");
  94.     }
  95.  
  96.     /**
  97.      * Close gc
  98.      * @return boolean Always true
  99.      */
  100.     public function gc($maxlifetime){
  101.         return true;
  102.     }
  103.  
  104.     /**
  105.      * Close session
  106.      * @return boolean Always true
  107.      */
  108.     public function close(){
  109.         return true;
  110.     }
  111. }
  112.  
  113. new memcacheSessionHandler();
  114. ?>
  115.  
  116. /* above is memcacheSessionHandler.class.php and next is your autoload.php, just load the memcacheSessionHandler.class.php file then do */
  117. $handler = new memcacheSessionHandler();
  118. session_set_save_handler($handler, true);
  119.  
  120. /* finally in php.ini I have the following */
  121. extension=php_memcache.dll
  122. [Memcache]
  123. memcache.allow_failover = 1
  124. memcache.max_failover_attempts=20
  125. memcache.chunk_size =8192
  126. memcache.default_port = 11211
  127.  
  128. /* so with memcached server/dameon running you'll now store your sessions to it instead of the default file. test.example
  129. <?php
  130. include_once(dirname(__FILE__).'/classes/autoload.php');
  131. session_start();
  132. $_SESSION['ID'] = 3;
  133. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement