Skip to content

Commit

Permalink
Changed write() to perform an insert on duplicate key update query, a…
Browse files Browse the repository at this point in the history
…s it was possible to update before and have nothing change, so we could not tell it apart from the case when the session had not been created.
  • Loading branch information
rikh42 committed Feb 25, 2013
1 parent 51fc9aa commit 938d1c8
Showing 1 changed file with 20 additions and 16 deletions.
36 changes: 20 additions & 16 deletions http/SessionStorageDb.php
Expand Up @@ -107,28 +107,32 @@ public function read($sessionID)



//==============================
// write
// called by PHP to write all the session data to the database
//==============================

/**
* called by PHP to write all the session data to the database
* @param $sessionID - the PHP session ID (basically a random string of characters)
* @param $data - The actual data to be stored
* @return bool - returns true
*/
public function write($sessionID, $data)
{
// Build the query to update the session in the DB
$sql = "UPDATE sessions SET sData=:data, iLastTouched=:time WHERE sSessionId=:sessionid LIMIT 1";
// prepare the data for the query
$param = array(
'text:sessionid' => $sessionID,
'text:data' => base64_encode($data),
'int:time' => time()
'id' => $sessionID,
'data' => base64_encode($data),
'time' => time()
);

// try and do it
$updateCount = $this->database->query($sql, $param);
if ($updateCount==0) {
// if we failed, it is because the session was not in the db, so add it
$this->startNewSession($sessionID, $data);
}
// duplicate some of the data for PDO
$param['udata'] = $param['data'];
$param['utime'] = $param['time'];

// yay
// Try and insert or update the data
$sql = "INSERT INTO sessions (sSessionId, sData, iLastTouched) VALUES (:id, :data, :time) "
. "ON DUPLICATE KEY UPDATE sData=:udata, iLastTouched=:utime ";

// do it.
$this->database->query($sql, $param);
return true;
}

Expand Down

0 comments on commit 938d1c8

Please sign in to comment.