我想将特定会话保存为路径。因此,如果我从MySQL数据库中删除它,会话也将被删除...
这可能吗? 谢谢。
答案 0 :(得分:0)
好的,这就是我所做的...请检查此代码是否正确,是否可以将$ _SESSION [' email']插入db作为路径...
<?php session_set_save_handler(
"openSession",
"closeSession",
"readSession",
"writeSession",
"destroySession",
"gcSession"
);
$mysqli = new mysqli("localhost", "root", "**password**", "**table**");
class Session {
/** @var Database */
private $mysqli;
}
function __construct()
{
// instance of new Database object
$this->mysqli = CDatabase::init(); //check this
// set handler to overide SESSION
@session_set_save_handler(
array($this, "openSession"),
array($this, "closeSession"),
array($this, "readSession"),
array($this, "writeSession"),
array($this, "destroySession"),
array($this, "gcSession")
);
// start the session
session_start();
}
public function openSession(
// if database connection exists
if($this->_db){
return true;
}
return false;
}
public function closeSession()
{
// if database connection is closed
if($this->mysqli->close()){
@session_write_close();
return true;
}
return false;
}
$email = $_SESSION['email'];
public function readSession($email)
{
$result = $this->mysqli->select(
"SELECT session_data FROM sessions WHERE session_id = :session_id",
array(":session_id"=>$email)
);
return isset($result[0]) ? $result[0] : "";
}
public function writeSession($email)
{
$result = $this->mysqli->insert(
"INSERT INTO table VALUES (session_id = :session_id)",
array(":session_id"=>$email)
);
if(isset($result[0])){
$result = $this->mysqli->update(
"sessions",
array(
"expires_at"=>time()+$this->getTimeout(),
"session_data"=>$data
),
"session_id = :session_id",
array(":session_id"=>$id)
);
}else{
$result = $this->_db->insert(
"sessions",
array(
"session_id"=>$email,
"expires_at"=>time()+$this->getTimeout()
)
);
}
return true;
}
public function destroySession($email)
{
return $this->_db->delete(
"sessions", "session_id = :session_id", array(":session_id"=>$email)
);
}
public function gcSession($maxLifetime)
{
return $this->_db->delete(
"sessions", "expires_at < :expires_at", array(":expires_at"=>time())
);
}
?>