-
-
Save pigeonator/939be131986e8dfa07f2 to your computer and use it in GitHub Desktop.
Saving PHP sessions in a database, using PDO.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Index.php | |
<?php | |
require_once("connection.php"); | |
require_once("database.class.php"); | |
require_once("session.class.php"); | |
$session = new Session; | |
if(!isset($_SESSION["counter"])){ | |
$_SESSION["counter"] = 0; | |
} | |
// Increase counter | |
echo $_SESSION["counter"]++; | |
?> | |
// connection.php | |
<?php | |
// Define database connection | |
define("DB_HOST", "localhost"); | |
define("DB_USER", "XXX"); | |
define("DB_PASS", "XXX"); | |
define("DB_NAME", "XXX"); | |
?> | |
// database.class.php | |
<?php | |
class Database { | |
private $host = DB_HOST; | |
private $user = DB_USER; | |
private $pass = DB_PASS; | |
private $dbname = DB_NAME; | |
private $dbh; | |
private $error; | |
private $stmt; | |
public function __construct() { | |
// Set DSN | |
$dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbname; | |
// Set options | |
$options = array( | |
PDO::ATTR_PERSISTENT => true, | |
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION | |
); | |
// Create a new PDO instanace | |
try { | |
$this->dbh = new PDO($dsn, $this->user, $this->pass, $options); | |
} catch(PDOException $e){ // Catch any errors | |
$this->error = $e->getMessage(); | |
} | |
} | |
public function query($query) { | |
$this->stmt = $this->dbh->prepare($query); | |
} | |
public function bind($param, $value, $type = null) { | |
if (is_null($type)) { | |
switch (true) { | |
case is_int($value): | |
$type = PDO::PARAM_INT; | |
break; | |
case is_bool($value): | |
$type = PDO::PARAM_BOOL; | |
break; | |
case is_null($value): | |
$type = PDO::PARAM_NULL; | |
break; | |
default: | |
$type = PDO::PARAM_STR; | |
} | |
} | |
$this->stmt->bindValue($param, $value, $type); | |
} | |
public function execute() { | |
return $this->stmt->execute(); | |
} | |
public function resultset() { | |
$this->execute(); | |
return $this->stmt->fetchAll(PDO::FETCH_ASSOC); | |
} | |
public function single() { | |
$this->execute(); | |
return $this->stmt->fetch(PDO::FETCH_ASSOC); | |
} | |
public function rowCount() { | |
return $this->stmt->rowCount(); | |
} | |
public function fetchColumn() { | |
$this->execute(); | |
return $this->stmt->fetchColumn(); | |
} | |
public function lastInsertId() { | |
return $this->dbh->lastInsertId(); | |
} | |
} // End class | |
?> | |
// session.class.php | |
<?php | |
class Session { | |
public function _construct(){ | |
// Instantiate new Database object | |
$this->db = new Database; | |
// Set handler to overide SESSION | |
session_set_save_handler( | |
array($this, "_open"), | |
array($this, "_close"), | |
array($this, "_read"), | |
array($this, "_write"), | |
array($this, "_destroy"), | |
array($this, "_gc") | |
); | |
// Start the session | |
session_start(); | |
} | |
/** | |
* Open | |
*/ | |
public function _open(){ | |
// If successful | |
if($this->db){ | |
// Return True | |
return true; | |
} | |
return false; | |
} | |
/** | |
* Close | |
*/ | |
public function _close(){ | |
// Close the database connection | |
// If successful | |
if($this->db->close()){ | |
// Return True | |
return true; | |
} | |
return false; | |
} | |
/** | |
* Read | |
*/ | |
public function _read($id){ | |
// Set query | |
$this->db->query('SELECT data FROM sessions WHERE id = :id'); | |
// Bind the Id | |
$this->db->bind(':id', $id); | |
// Attempt execution | |
// If successful | |
if($this->db->execute()){ | |
// Save returned row | |
$row = $this->db->single(); | |
// Return the data | |
return $row['data']; | |
}else{ | |
// Return an empty string | |
return ''; | |
} | |
} | |
/** | |
* Write | |
*/ | |
public function _write($id, $data){ | |
// Create time stamp | |
$access = time(); | |
// Set query | |
$this->db->query('REPLACE INTO sessions VALUES (:id, :access, :data)'); | |
// Bind data | |
$this->db->bind(':id', $id); | |
$this->db->bind(':access', $access); | |
$this->db->bind(':data', $data); | |
// Attempt Execution | |
// If successful | |
if($this->db->execute()){ | |
// Return True | |
return true; | |
} | |
return false; | |
} | |
/** | |
* Destroy | |
*/ | |
public function _destroy($id){ | |
// Set query | |
$this->db->query('DELETE FROM sessions WHERE id = :id'); | |
// Bind data | |
$this->db->bind(':id', $id); | |
// Attempt execution | |
// If successful | |
if($this->db->execute()){ | |
// Return True | |
return true; | |
} | |
return false; | |
} | |
/** | |
* Garbage Collection | |
*/ | |
public function _gc($max){ | |
// Calculate what is to be deemed old | |
$old = time() - $max; | |
// Set query | |
$this->db->query('DELETE * FROM sessions WHERE access < :old'); | |
// Bind data | |
$this->db->bind(':old', $old); | |
// Attempt execution | |
if($this->db->execute()){ | |
// Return True | |
return true; | |
} | |
return false; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment