├── README.md ├── example.php └── MySqlSessionHandler.php /README.md: -------------------------------------------------------------------------------- 1 | # PHP MySQL Session Handler 2 | 3 | This is old and abandoned. Do not use. 4 | -------------------------------------------------------------------------------- /example.php: -------------------------------------------------------------------------------- 1 | setDbDetails('localhost', 'username', 'password', 'database'); 8 | 9 | // OR alternatively send a MySQLi ressource 10 | // $session->setDbConnection($mysqli); 11 | 12 | $session->setDbTable('session_handler_table'); 13 | session_set_save_handler(array($session, 'open'), 14 | array($session, 'close'), 15 | array($session, 'read'), 16 | array($session, 'write'), 17 | array($session, 'destroy'), 18 | array($session, 'gc')); 19 | 20 | // The following prevents unexpected effects when using objects as save handlers. 21 | register_shutdown_function('session_write_close'); 22 | 23 | session_start(); 24 | -------------------------------------------------------------------------------- /MySqlSessionHandler.php: -------------------------------------------------------------------------------- 1 | 7 | * @link https://github.com/sprain/PHP-MySQL-Session-Handler 8 | */ 9 | 10 | class MySqlSessionHandler{ 11 | 12 | /** 13 | * a database MySQLi connection resource 14 | * @var resource 15 | */ 16 | protected $dbConnection; 17 | 18 | /** 19 | * the name of the DB table which handles the sessions 20 | * @var string 21 | */ 22 | protected $dbTable; 23 | 24 | /** 25 | * Set db data if no connection is being injected 26 | * @param string $dbHost 27 | * @param string $dbUser 28 | * @param string $dbPassword 29 | * @param string $dbDatabase 30 | */ 31 | public function setDbDetails($dbHost, $dbUser, $dbPassword, $dbDatabase) 32 | { 33 | $this->dbConnection = new mysqli($dbHost, $dbUser, $dbPassword, $dbDatabase); 34 | 35 | if (mysqli_connect_error()) { 36 | throw new Exception('Connect Error (' . mysqli_connect_errno() . ') ' . mysqli_connect_error()); 37 | } 38 | } 39 | 40 | /** 41 | * Inject DB connection from outside 42 | * @param object $dbConnection expects MySQLi object 43 | */ 44 | public function setDbConnection($dbConnection) 45 | { 46 | $this->dbConnection = $dbConnection; 47 | } 48 | 49 | /** 50 | * Inject DB connection from outside 51 | * @param object $dbConnection expects MySQLi object 52 | */ 53 | public function setDbTable($dbTable) 54 | { 55 | $this->dbTable = $dbTable; 56 | } 57 | 58 | /** 59 | * Open the session 60 | * @return bool 61 | */ 62 | public function open() 63 | { 64 | //delete old session handlers 65 | $limit = time() - (3600 * 24); 66 | $sql = sprintf("DELETE FROM %s WHERE timestamp < %s", $this->dbTable, $limit); 67 | return $this->dbConnection->query($sql); 68 | } 69 | 70 | /** 71 | * Close the session 72 | * @return bool 73 | */ 74 | public function close() 75 | { 76 | return $this->dbConnection->close(); 77 | } 78 | 79 | /** 80 | * Read the session 81 | * @param int session id 82 | * @return string string of the sessoin 83 | */ 84 | public function read($id) 85 | { 86 | $sql = sprintf("SELECT data FROM %s WHERE id = '%s'", $this->dbTable, $this->dbConnection->escape_string($id)); 87 | if ($result = $this->dbConnection->query($sql)) { 88 | if ($result->num_rows && $result->num_rows > 0) { 89 | $record = $result->fetch_assoc(); 90 | return $record['data']; 91 | } else { 92 | return false; 93 | } 94 | } else { 95 | return false; 96 | } 97 | 98 | return true; 99 | } 100 | 101 | /** 102 | * Write the session 103 | * @param int session id 104 | * @param string data of the session 105 | */ 106 | public function write($id, $data) 107 | { 108 | 109 | $sql = sprintf("REPLACE INTO %s VALUES('%s', '%s', '%s')", 110 | $this->dbTable, 111 | $this->dbConnection->escape_string($id), 112 | $this->dbConnection->escape_string($data), 113 | time()); 114 | return $this->dbConnection->query($sql); 115 | } 116 | 117 | /** 118 | * Destoroy the session 119 | * @param int session id 120 | * @return bool 121 | */ 122 | public function destroy($id) 123 | { 124 | $sql = sprintf("DELETE FROM %s WHERE `id` = '%s'", $this->dbTable, $this->dbConnection->escape_string($id)); 125 | return $this->dbConnection->query($sql); 126 | } 127 | 128 | /** 129 | * Garbage Collector 130 | * @param int life time (sec.) 131 | * @return bool 132 | * @see session.gc_divisor 100 133 | * @see session.gc_maxlifetime 1440 134 | * @see session.gc_probability 1 135 | * @usage execution rate 1/100 136 | * (session.gc_probability/session.gc_divisor) 137 | */ 138 | public function gc($max) 139 | { 140 | $sql = sprintf("DELETE FROM %s WHERE `timestamp` < '%s'", $this->dbTable, time() - intval($max)); 141 | return $this->dbConnection->query($sql); 142 | } 143 | } 144 | --------------------------------------------------------------------------------