CDbHttpSession
鍖 | system.web |
---|---|
缁ф壙 | class CDbHttpSession » CHttpSession » CApplicationComponent » CComponent |
瀹炵幇 | Countable, ArrayAccess, Traversable, IteratorAggregate, IApplicationComponent |
婧愯嚜 | 1.0 |
鐗堟湰 | $Id: CDbHttpSession.php 3426 2011-10-25 00:01:09Z alexander.makarow $ |
婧愮爜 | framework/web/CDbHttpSession.php |
CDbHttpSession stores session data in a DB table named 'YiiSession'. The table name can be changed by setting sessionTableName. If the table does not exist, it will be automatically created if autoCreateSessionTable is set true.
The following is the table structure:
CREATE TABLE YiiSession ( id CHAR(32) PRIMARY KEY, expire INTEGER, data TEXT )
CDbHttpSession relies on PDO to access database.
By default, it will use an SQLite3 database named 'session-YiiVersion.db' under the application runtime directory. You can also specify connectionID so that it makes use of a DB application component to access database.
When using CDbHttpSession in a production server, we recommend you pre-create the session DB table and set autoCreateSessionTable to be false. This will greatly improve the performance. You may also create a DB index for the 'expire' column in the session table to further improve the performance.
鍏叡灞炴
灞炴 | 绫诲瀷 | 鎻忚堪 | 瀹氫箟鍦 |
---|---|---|---|
autoCreateSessionTable | boolean | whether the session DB table should be automatically created if not exists. | CDbHttpSession |
autoStart | boolean | whether the session should be automatically started when the session application component is initialized, defaults to true. | CHttpSession |
behaviors | array | 杩欎釜搴旂敤缁勪欢闄勫姞鐨勮涓恒 杩欐琛屼负灏嗗湪搴旂敤缁勪欢璋冪敤init鏃堕檮鍔犲湪搴旂敤缁勪欢涓娿 璇峰弬鐓CModel::behaviors濡備綍鎸囧畾姝ゅ睘鎬у笺 | CApplicationComponent |
connectionID | string | the ID of a CDbConnection application component. | CDbHttpSession |
cookieMode | string | how to use cookie to store session ID. | CHttpSession |
cookieParams | array | the session cookie parameters. | CHttpSession |
count | integer | Returns the number of items in the session. | CHttpSession |
gCProbability | integer | the probability (percentage) that the gc (garbage collection) process is started on every session initialization, defaults to 1 meaning 1% chance. | CHttpSession |
isInitialized | boolean | 妫鏌ュ簲鐢ㄧ粍浠舵槸鍚﹀凡缁忓垵濮嬪寲銆 | CApplicationComponent |
isStarted | boolean | whether the session has started | CHttpSession |
iterator | CHttpSessionIterator | Returns an iterator for traversing the session variables. | CHttpSession |
keys | array | the list of session variable names | CHttpSession |
savePath | string | the current session save path, defaults to '/tmp'. | CHttpSession |
sessionID | string | the current session ID | CHttpSession |
sessionName | string | the current session name | CHttpSession |
sessionTableName | string | the name of the DB table to store session content. | CDbHttpSession |
timeout | integer | the number of seconds after which data will be seen as 'garbage' and cleaned up, defaults to 1440 seconds. | CHttpSession |
useCustomStorage | boolean | Returns a value indicating whether to use custom session storage. | CDbHttpSession |
useTransparentSessionID | boolean | whether transparent sid support is enabled or not, defaults to false. | CHttpSession |
鍏叡鏂规硶
鏂规硶 | 鎻忚堪 | 瀹氫箟鍦 |
---|---|---|
__call() | 濡傛灉绫讳腑娌℃湁璋冪殑鏂规硶鍚嶏紝鍒欒皟鐢ㄨ繖涓柟娉曘 | CComponent |
__get() | 杩斿洖涓涓睘鎬у笺佷竴涓簨浠跺鐞嗙▼搴忓垪琛ㄦ垨涓涓涓哄悕绉般 | CComponent |
__isset() | 妫鏌ヤ竴涓睘鎬ф槸鍚︿负null銆 | CComponent |
__set() | 璁剧疆涓涓粍浠剁殑灞炴у笺 | CComponent |
__unset() | 璁剧疆涓涓粍浠剁殑灞炴т负null銆 | CComponent |
add() | Adds a session variable. | CHttpSession |
asa() | 杩斿洖杩欎釜鍚嶅瓧鐨勮涓哄璞° | CComponent |
attachBehavior() | 闄勫姞涓涓涓哄埌缁勪欢銆 | CComponent |
attachBehaviors() | 闄勫姞涓涓涓哄垪琛ㄥ埌缁勪欢銆 | CComponent |
attachEventHandler() | 涓轰簨浠堕檮鍔犱竴涓簨浠跺鐞嗙▼搴忋 | CComponent |
canGetProperty() | 纭畾灞炴ф槸鍚﹀彲璇汇 | CComponent |
canSetProperty() | 纭畾灞炴ф槸鍚﹀彲鍐欍 | CComponent |
clear() | Removes all session variables | CHttpSession |
close() | Ends the current session and store session data. | CHttpSession |
closeSession() | Session close handler. | CHttpSession |
contains() | CHttpSession | |
count() | Returns the number of items in the session. | CHttpSession |
destroy() | Frees all session variables and destroys all data registered to a session. | CHttpSession |
destroySession() | Session destroy handler. | CDbHttpSession |
detachBehavior() | 浠庣粍浠朵腑鍒嗙涓涓涓恒 | CComponent |
detachBehaviors() | 浠庣粍浠朵腑鍒嗙鎵鏈夎涓恒 | CComponent |
detachEventHandler() | 鍒嗙涓涓瓨鍦ㄧ殑浜嬩欢澶勭悊绋嬪簭銆 | CComponent |
disableBehavior() | 绂佺敤涓涓檮鍔犺涓恒 | CComponent |
disableBehaviors() | 绂佺敤缁勪欢闄勫姞鐨勬墍鏈夎涓恒 | CComponent |
enableBehavior() | 鍚敤涓涓檮鍔犺涓恒 | CComponent |
enableBehaviors() | 鍚敤缁勪欢闄勫姞鐨勬墍鏈夎涓恒 | CComponent |
evaluateExpression() | 璁$畻涓涓狿HP琛ㄨ揪寮忥紝鎴栨牴鎹粍浠朵笂涓嬫枃鎵ц鍥炶皟銆 | CComponent |
gcSession() | Session GC (garbage collection) handler. | CDbHttpSession |
get() | Returns the session variable value with the session variable name. | CHttpSession |
getCookieMode() | 杩斿洖how to use cookie to store session ID. Defaults to 'Allow'. | CHttpSession |
getCookieParams() | 杩斿洖the session cookie parameters. | CHttpSession |
getCount() | Returns the number of items in the session. | CHttpSession |
getEventHandlers() | 杩斿洖涓涓簨浠剁殑闄勫姞澶勭悊绋嬪簭鍒楄〃銆 | CComponent |
getGCProbability() | 杩斿洖the probability (percentage) that the gc (garbage collection) process is started on every session initialization, defaults to 1 meaning 1% chance. | CHttpSession |
getIsInitialized() | 妫鏌ュ簲鐢ㄧ粍浠舵槸鍚﹀凡缁忓垵濮嬪寲銆 | CApplicationComponent |
getIsStarted() | 妫鏌hether the session has started | CHttpSession |
getIterator() | Returns an iterator for traversing the session variables. | CHttpSession |
getKeys() | 杩斿洖the list of session variable names | CHttpSession |
getSavePath() | 杩斿洖the current session save path, defaults to '/tmp'. | CHttpSession |
getSessionID() | 杩斿洖the current session ID | CHttpSession |
getSessionName() | 杩斿洖the current session name | CHttpSession |
getTimeout() | 杩斿洖the number of seconds after which data will be seen as 'garbage' and cleaned up, defaults to 1440 seconds. | CHttpSession |
getUseCustomStorage() | Returns a value indicating whether to use custom session storage. | CDbHttpSession |
getUseTransparentSessionID() | 杩斿洖whether transparent sid support is enabled or not, defaults to false. | CHttpSession |
hasEvent() | 纭畾涓涓簨浠舵槸鍚﹀畾涔夈 | CComponent |
hasEventHandler() | 妫鏌ヤ簨浠舵槸鍚︽湁闄勫姞鐨勫鐞嗙▼搴忋 | CComponent |
hasProperty() | 纭畾灞炴ф槸鍚﹁瀹氫箟銆 | CComponent |
init() | Initializes the application component. | CHttpSession |
itemAt() | Returns the session variable value with the session variable name. | CHttpSession |
offsetExists() | This method is required by the interface ArrayAccess. | CHttpSession |
offsetGet() | This method is required by the interface ArrayAccess. | CHttpSession |
offsetSet() | This method is required by the interface ArrayAccess. | CHttpSession |
offsetUnset() | This method is required by the interface ArrayAccess. | CHttpSession |
open() | Starts the session if it has not started yet. | CHttpSession |
openSession() | Session open handler. | CDbHttpSession |
raiseEvent() | 鍙戣捣涓涓簨浠躲 | CComponent |
readSession() | Session read handler. | CDbHttpSession |
regenerateID() | Updates the current session id with a newly generated one. | CDbHttpSession |
remove() | Removes a session variable. | CHttpSession |
setCookieMode() | 璁剧疆how to use cookie to store session ID. Valid values include 'none', 'allow' and 'only'. | CHttpSession |
setCookieParams() | Sets the session cookie parameters. | CHttpSession |
setGCProbability() | 璁剧疆the probability (percentage) that the gc (garbage collection) process is started on every session initialization. | CHttpSession |
setSavePath() | 璁剧疆the current session save path | CHttpSession |
setSessionID() | 璁剧疆the session ID for the current session | CHttpSession |
setSessionName() | 璁剧疆the session name for the current session, must be an alphanumeric string, defaults to PHPSESSID | CHttpSession |
setTimeout() | 璁剧疆the number of seconds after which data will be seen as 'garbage' and cleaned up | CHttpSession |
setUseTransparentSessionID() | 璁剧疆whether transparent sid support is enabled or not. | CHttpSession |
toArray() | CHttpSession | |
writeSession() | Session write handler. | CDbHttpSession |
鍙椾繚鎶ゆ柟娉
鏂规硶 | 鎻忚堪 | 瀹氫箟鍦 |
---|---|---|
createSessionTable() | Creates the session DB table. | CDbHttpSession |
getDbConnection() | 杩斿洖the DB connection instance | CDbHttpSession |
灞炴ц缁
whether the session DB table should be automatically created if not exists. Defaults to true.
鍙傝
the ID of a CDbConnection application component. If not set, a SQLite database
will be automatically created and used. The SQLite database file is
is protected/runtime/session-YiiVersion.db
.
the DB connection instance
the name of the DB table to store session content. Note, if autoCreateSessionTable is false and you want to create the DB table manually by yourself, you need to make sure the DB table is of the following structure:
(id CHAR(32) PRIMARY KEY, expire INTEGER, data TEXT)
Returns a value indicating whether to use custom session storage. This method overrides the parent implementation and always returns true.
鏂规硶璇︾粏
protected void createSessionTable(CDbConnection $db, string $tableName)
| ||
$db | CDbConnection | the database connection |
$tableName | string | the name of the table to be created |
protected function createSessionTable($db,$tableName)
{
$sql="
CREATE TABLE $tableName
(
id CHAR(32) PRIMARY KEY,
expire INTEGER,
data TEXT
)";
$db->createCommand($sql)->execute();
}
Creates the session DB table.
public boolean destroySession(string $id)
| ||
$id | string | session ID |
{return} | boolean | whether session is destroyed successfully |
public function destroySession($id)
{
$sql="DELETE FROM {$this->sessionTableName} WHERE id=:id";
$this->getDbConnection()->createCommand($sql)->bindValue(':id',$id)->execute();
return true;
}
Session destroy handler. Do not call this method directly.
public boolean gcSession(integer $maxLifetime)
| ||
$maxLifetime | integer | the number of seconds after which data will be seen as 'garbage' and cleaned up. |
{return} | boolean | whether session is GCed successfully |
public function gcSession($maxLifetime)
{
$sql="DELETE FROM {$this->sessionTableName} WHERE expire<".time();
$this->getDbConnection()->createCommand($sql)->execute();
return true;
}
Session GC (garbage collection) handler. Do not call this method directly.
protected CDbConnection getDbConnection()
| ||
{return} | CDbConnection | the DB connection instance |
protected function getDbConnection()
{
if($this->_db!==null)
return $this->_db;
else if(($id=$this->connectionID)!==null)
{
if(($this->_db=Yii::app()->getComponent($id)) instanceof CDbConnection)
return $this->_db;
else
throw new CException(Yii::t('yii','CDbHttpSession.connectionID "{id}" is invalid. Please make sure it refers to the ID of a CDbConnection application component.',
array('{id}'=>$id)));
}
else
{
$dbFile=Yii::app()->getRuntimePath().DIRECTORY_SEPARATOR.'session-'.Yii::getVersion().'.db';
return $this->_db=new CDbConnection('sqlite:'.$dbFile);
}
}
public boolean getUseCustomStorage()
| ||
{return} | boolean | whether to use custom storage. |
Returns a value indicating whether to use custom session storage. This method overrides the parent implementation and always returns true.
public boolean openSession(string $savePath, string $sessionName)
| ||
$savePath | string | session save path |
$sessionName | string | session name |
{return} | boolean | whether session is opened successfully |
public function openSession($savePath,$sessionName)
{
if($this->autoCreateSessionTable)
{
$db=$this->getDbConnection();
$db->setActive(true);
$sql="DELETE FROM {$this->sessionTableName} WHERE expire<".time();
try
{
$db->createCommand($sql)->execute();
}
catch(Exception $e)
{
$this->createSessionTable($db,$this->sessionTableName);
}
}
return true;
}
Session open handler. Do not call this method directly.
public string readSession(string $id)
| ||
$id | string | session ID |
{return} | string | the session data |
public function readSession($id)
{
$now=time();
$sql="
SELECT data FROM {$this->sessionTableName}
WHERE expire>$now AND id=:id
";
$data=$this->getDbConnection()->createCommand($sql)->bindValue(':id',$id)->queryScalar();
return $data===false?'':$data;
}
Session read handler. Do not call this method directly.
public void regenerateID(boolean $deleteOldSession=false)
| ||
$deleteOldSession | boolean | Whether to delete the old associated session file or not. |
public function regenerateID($deleteOldSession=false)
{
$oldID=session_id();
// if no session is started, there is nothing to regenerate
if(empty($oldID))
return;
parent::regenerateID(false);
$newID=session_id();
$db=$this->getDbConnection();
$sql="SELECT * FROM {$this->sessionTableName} WHERE id=:id";
$row=$db->createCommand($sql)->bindValue(':id',$oldID)->queryRow();
if($row!==false)
{
if($deleteOldSession)
{
$sql="UPDATE {$this->sessionTableName} SET id=:newID WHERE id=:oldID";
$db->createCommand($sql)->bindValue(':newID',$newID)->bindValue(':oldID',$oldID)->execute();
}
else
{
$row['id']=$newID;
$db->createCommand()->insert($this->sessionTableName, $row);
}
}
else
{
// shouldn't reach here normally
$db->createCommand()->insert($this->sessionTableName, array(
'id'=>$newID,
'expire'=>time()+$this->getTimeout(),
));
}
}
Updates the current session id with a newly generated one. Please refer to http://php.net/session_regenerate_id for more details.
public boolean writeSession(string $id, string $data)
| ||
$id | string | session ID |
$data | string | session data |
{return} | boolean | whether session write is successful |
public function writeSession($id,$data)
{
// exception must be caught in session write handler
// http://us.php.net/manual/en/function.session-set-save-handler.php
try
{
$expire=time()+$this->getTimeout();
$db=$this->getDbConnection();
$sql="SELECT id FROM {$this->sessionTableName} WHERE id=:id";
if($db->createCommand($sql)->bindValue(':id',$id)->queryScalar()===false)
$sql="INSERT INTO {$this->sessionTableName} (id, data, expire) VALUES (:id, :data, $expire)";
else
$sql="UPDATE {$this->sessionTableName} SET expire=$expire, data=:data WHERE id=:id";
$db->createCommand($sql)->bindValue(':id',$id)->bindValue(':data',$data)->execute();
}
catch(Exception $e)
{
if(YII_DEBUG)
echo $e->getMessage();
// it is too late to log an error message here
return false;
}
return true;
}
Session write handler. Do not call this method directly.