![](/img/trans.png)
[英]How do I get a value from PHP's defualt session when a custom session handler is in use?
[英]Custom session handler - how to use it correctly?
測試此程序時,我得到了一些奇怪的結果,但似乎無法弄清楚如何使用它。 我有這個自定義會話處理程序:
require_once('database.php');
Class linusLogin_Session {
private $_database;
private $_key = '*withheld*';
public function __construct() {
$this->_database = new linusLogin_Database;
session_set_save_handler(
array($this, "_open"),
array($this, "_close"),
array($this, "_read"),
array($this, "_write"),
array($this, "_destroy"),
array($this, "_gc")
);
session_name('llSSID');
session_start();
}
public function _open(){
return TRUE;
}
public function _close(){
return $this->_database->close();
}
public function _read($id){
$data = null;
$result = $this->_database->selectSessData($id);
if ($result !== FALSE) {
$result = $this->_decrypt($result);
return $result;
}
else
return "";
}
public function _write($id, $data){
$access = time();
$data = $this->_encrypt($data);
$result = $this->_database->replaceSessData($id, $access, $data);
}
public function _destroy($id){
return $this->_database->destroySessData($id);
}
public function _gc($max){
$old = time() - $max;
return $this->_database->gCollectSessData($old);
}
private function _encrypt($data) {
*encryption withheld*
return $data;
}
private function _decrypt($data) {
*decryption withheld*
return $data;
}
public function setKey($key) {
$this->_key = $key;
}
}
$session = new linusLogin_Session;
因此,在文件實例化它是自己的類時,我在這里是否正確(最后一行)? 還是在我使用session_start()
時實例化該類? 我在結構中有session_start()
,我希望它是所有文檔中唯一的位置-這是一個好習慣嗎? 我之前在此處理程序上遇到過麻煩,原因是即使在處理程序中也沒有在頁面上使用session_start()
。 (我真的很討厭那些說
session_start()
已被調用,忽略session_start()
附帶的問題-我一直在聽我應該為每個請求重新生成session id
但是請求是什么意思? 這是否意味着每個http請求還是會話中的每個更改? 生成ID的最佳實踐是什么(不使用“ Every request ”行)。
此外,關於安全性的任何建議都將是很棒的。
自定義會話處理程序由PHP的會話代碼使用。 這是要執行的對象,而不是演員。 因此,您應該只定義自定義方法,然后在其他地方開始會話。
例如:
// Make sure you make changes like this one:
Class linusLogin_Session implements SessionHandlerInterface {
// This doesn't need to do anything with session config or call any session_* functions
public function __construct() {
$this->_database = new linusLogin_Database;
}
}
接着...
include "mysessionhandler.php";
$session = new linusLogin_Session();
session_set_save_handler($session);
session_name('llSSID');
session_start();
// The rest of your site goes here
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.