Skip to content

Instantly share code, notes, and snippets.

@mumumu
Created May 7, 2010 11:22
Show Gist options
  • Save mumumu/393300 to your computer and use it in GitHub Desktop.
Save mumumu/393300 to your computer and use it in GitHub Desktop.
<?php
class My_Session_SaveHandler_DbTable extends Zend_Session_SaveHandler_DbTable
{
/**
* Read session data
*
* @param string $id
* @return string
*/
public function read($id)
{
$return = '';
$db = $this->getAdapter();
$select_stmt = $db->prepare('SELECT * FROM ' . $this->_name . ' WHERE id = ?');
$select_stmt->execute(array($id));
$select_stmt->bindColumn(1, $id, PDO::PARAM_STR);
$select_stmt->bindColumn(2, $modified, PDO::PARAM_INT);
$select_stmt->bindColumn(3, $lifetime, PDO::PARAM_INT);
$select_stmt->bindColumn(4, $data, PDO::PARAM_STR); // bytea を stringとして扱う
$result = $select_stmt->fetch(PDO::FETCH_BOUND);
if ($result === true) {
$expirationtime = (int)$modified + (int)$lifetime;
if ($expirationtime > time()) {
$return = pg_unescape_bytea($data);
} else {
$this->destroy($id);
}
}
return $return;
}
/**
* Write session data
*
* @param string $id
* @param string $data
* @return boolean
*/
public function write($id, $data)
{
$return = false;
$nowtime = time();
$data = pg_escape_bytea((string)$data);
$rows = call_user_func_array(array(&$this, 'find'), $this->_getPrimary($id));
$db = $this->getAdapter();
if (count($rows)) {
$lifetime = $this->_getLifetime($rows->current());
$update_stmt = $db->prepare('UPDATE ' . $this->_name
. ' SET ' . $this->_modifiedColumn
. ' = ?, ' . $this->_lifetimeColumn
. ' = ?, ' . $this->_dataColumn
. ' = ? '
. ' WHERE id = ?'
);
$update_stmt->bindParam(1, $nowtime, PDO::PARAM_INT);
$update_stmt->bindParam(2, $lifetime, PDO::PARAM_INT);
// bytea を string として扱う
$update_stmt->bindParam(3, $data, PDO::PARAM_STR);
$update_stmt->bindParam(4, $id, PDO::PARAM_STR);
$update_stmt->execute();
$return = true;
} else {
$insert_stmt = $db->prepare('INSERT INTO ' . $this->_name . ' VALUES (?, ?, ?, ?)');
$insert_stmt->bindParam(1, $id, PDO::PARAM_STR);
$insert_stmt->bindParam(2, $nowtime, PDO::PARAM_INT);
$insert_stmt->bindParam(3, $this->_lifetime, PDO::PARAM_INT);
// bytea を string として扱う
$insert_stmt->bindParam(4, $data, PDO::PARAM_STR);
$insert_stmt->execute();
$return = true;
}
return $return;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment