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

Namespaces

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

Classes

  • AbstractCacheSession
  • AbstractSession
  • MemcachedSession
  • MySQLSession
  • RedisSession
  1 <?php
  2 
  3     namespace Alo\Session;
  4 
  5     use Alo;
  6     use Alo\Db\MySQL;
  7     use Alo\Exception\LibraryException as Libex;
  8 
  9     if (!defined('GEN_START')) {
 10         http_response_code(404);
 11     } else {
 12 
 13         /**
 14          * MySQL-based session handler. The ALO_SESSION_CLEANUP constant is not used here as session should be cleaned up
 15          * by the MySQL event scheduler.
 16          *
 17          * @author Art <a.molcanovas@gmail.com>
 18          */
 19         class MySQLSession extends AbstractSession {
 20 
 21             /**
 22              * Database instance
 23              *
 24              * @var MySQL
 25              */
 26             protected $db;
 27 
 28             /**
 29              * Constructor
 30              *
 31              * @author Art <a.molcanovas@gmail.com>
 32              * @throws Libex When $instance is not passed and Alo::$db does not contain a MySQL instance
 33              *
 34              * @param MySQL $instance If a parameter is passed here its instance will be used instead of Alo::$db
 35              */
 36             function __construct(MySQL &$instance = null) {
 37                 if ($instance) {
 38                     $this->db = &$instance;
 39                 } elseif (Alo::$db && Alo::$db instanceof MySQL) {
 40                     $this->db = &Alo::$db;
 41                 } else {
 42                     throw new Libex('MySQL instance not found', Libex::E_REQUIRED_LIB_NOT_FOUND);
 43                 }
 44 
 45                 parent::__construct();
 46             }
 47 
 48             /**
 49              * Destroys a session
 50              * @author Art <a.molcanovas@gmail.com>
 51              *
 52              * @param string $sessionID The ID to destroy
 53              *
 54              * @return bool
 55              */
 56             public function destroy($sessionID) {
 57                 parent::destroy($sessionID);
 58 
 59                 return $this->db->prepQuery('DELETE FROM `' . ALO_SESSION_TABLE_NAME . '` WHERE `id`=?', [$sessionID]);
 60             }
 61 
 62             /**
 63              * Initialises a MySQLSession
 64              * @author Art <a.molcanovas@gmail.com>
 65              *
 66              * @param MySQL $dependcyObject If you don't want to use Alo::$db you can pass a MySQL instance reference here.
 67              */
 68             static function init(MySQL &$dependcyObject = null) {
 69                 parent::initSession($dependcyObject, get_class());
 70             }
 71 
 72             /**
 73              * Read ssession data
 74              *
 75              * @author Art <a.molcanovas@gmail.com>
 76              * @link   http://php.net/manual/en/sessionhandlerinterface.read.php
 77              *
 78              * @param string $sessionID The session id to read data for.
 79              *
 80              * @return string
 81              */
 82             public function read($sessionID) {
 83                 $data = $this->db->prepQuery('SELECT `data` FROM `' . ALO_SESSION_TABLE_NAME . '` WHERE `id`=?',
 84                                              [$sessionID]);
 85 
 86                 return $data ? $data[0]['data'] : '';
 87             }
 88 
 89             /**
 90              * Write session data
 91              *
 92              * @author Art <a.molcanovas@gmail.com>
 93              * @link   http://php.net/manual/en/sessionhandlerinterface.write.php
 94              *
 95              * @param string $sessionID    The session id.
 96              * @param string $sessionData  The encoded session data. This data is the
 97              *                             result of the PHP internally encoding
 98              *                             the $_SESSION superglobal to a serialized
 99              *                             string and passing it as this parameter.
100              *                             Please note sessions use an alternative serialization method.
101              *
102              * @return bool
103              */
104             public function write($sessionID, $sessionData) {
105                 return $this->db->prepQuery('INSERT INTO `' . ALO_SESSION_TABLE_NAME . '`(' . '`id`,' . '`data`,' .
106                                             '`access`) VALUES(:id,:data,CURRENT_TIMESTAMP) ' .
107                                             'ON DUPLICATE KEY UPDATE ' . '`data`=VALUES(`data`),' .
108                                             '`access`=CURRENT_TIMESTAMP', [':id'   => $sessionID,
109                                                                            ':data' => $sessionData]);
110             }
111         }
112     }
113 
AloFramework documentation API documentation generated byApiGen 2.8.0