gpt4 book ai didi

php - 如何在php中获取特定的 session 路径?

转载 作者:行者123 更新时间:2023-11-29 22:55:32 24 4
gpt4 key购买 nike

我想将特定 session 保存为路径。所以如果我从 MySQL 数据库中删除它, session 也会被删除......

这可能吗?谢谢。

最佳答案

好的,这就是我所做的...请检查此代码是否正确,将能够将 $_SESSION['email'] 作为路径插入到数据库...

   <?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())
);
}

?>

关于php - 如何在php中获取特定的 session 路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28731014/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com