AloFramework documentation
  • Namespace
  • Class
  • Tree
  • Deprecated
  • Todo
  • Download

Namespaces

  • Alo
    • Cache
    • CLI
    • Controller
    • Db
    • Exception
    • FileSystem
    • Session
    • Statics
    • Test
    • Validators
    • Windows
  • Controller
  • None
  • PHP

Classes

  • AbstractCacheSession
  • AbstractSession
  • MemcachedSession
  • RedisSession
  • SQLSession
  1 <?php
  2 
  3    namespace Alo\Session;
  4 
  5    use Alo;
  6    use Alo\Db\MySQL;
  7 
  8    if (!defined('GEN_START')) {
  9       http_response_code(404);
 10       die();
 11    }
 12 
 13    /**
 14     * The SQL-based session handler. ALO_SESSION_CLEANUP is not used here as
 15     * cleanup is handled by the MySQL event handler
 16     *
 17     * @author  Art <a.molcanovas@gmail.com>
 18     * @package Session
 19     */
 20    class SQLSession extends AbstractSession {
 21 
 22       /**
 23        * Reference to database instance
 24        *
 25        * @var MySQL
 26        */
 27       protected $db;
 28 
 29       /**
 30        * Instantiates the class
 31        *
 32        * @author Art <a.molcanovas@gmail.com>
 33        */
 34       function __construct() {
 35          if (!Alo::$db) {
 36             Alo::$db = new MySQL();
 37          }
 38 
 39          $this->db = &Alo::$db;
 40          parent::__construct();
 41          \Log::debug('Initialised MySQL session');
 42       }
 43 
 44       /**
 45        * Instantiates the class
 46        *
 47        * @author Art <a.molcanovas@gmail.com>
 48        *
 49        * @return SQLSession
 50        */
 51       static function SQLSession() {
 52          return new SQLSession();
 53       }
 54 
 55       /**
 56        * Fetches session data
 57        *
 58        * @author Art <a.molcanovas@gmail.com>
 59        * @return SQLSession
 60        */
 61       protected function fetch() {
 62          $sql = $this->db->prepQuery('SELECT `data` '
 63                                      . 'FROM `' . ALO_SESSION_TABLE_NAME . '` '
 64                                      . 'WHERE `id`=? '
 65                                      . 'LIMIT 1', [$this->id], [
 66                                         MySQL::V_CACHE => false
 67                                      ]);
 68 
 69          if (!empty($sql)) {
 70             $this->data = json_decode($sql[0]['data'], true);
 71          }
 72 
 73          \Log::debug('Saved session data');
 74 
 75          return $this;
 76       }
 77 
 78       /**
 79        * Terminates the session
 80        *
 81        * @author Art <a.molcanovas@gmail.com>
 82        * @return SQLSession
 83        */
 84       function terminate() {
 85          $this->db->prepQuery('DELETE FROM `' . ALO_SESSION_TABLE_NAME . '` '
 86                               . 'WHERE `id`=? '
 87                               . 'LIMIT 1', [$this->id], [MySQL::V_CACHE => false]);
 88 
 89          return parent::terminate();
 90       }
 91 
 92       /**
 93        * Saves session data
 94        *
 95        * @author Art <a.molcanovas@gmail.com>
 96        * @return SQLSession
 97        */
 98       protected function write() {
 99          $this->db->prepQuery('REPLACE INTO `'
100                               . ALO_SESSION_TABLE_NAME . '`(`id`,`data`,`access`) VALUES('
101                               . '?,?,CURRENT_TIMESTAMP)', [
102                                  $this->id,
103                                  json_encode($this->data)
104                               ], [MySQL::V_CACHE => false]);
105 
106          \Log::debug('Saved session data');
107 
108          return $this;
109       }
110 
111    }
AloFramework documentation API documentation generated by ApiGen 2.8.0