gpt4 book ai didi

php - session 处理类奇怪的行为

转载 作者:行者123 更新时间:2023-11-28 23:38:21 24 4
gpt4 key购买 nike

我正在使用以下类来处理 session :

class DBHandler implements SessionHandlerInterface {

public function open($save_path, $name) {
try {
DBCxn::get();
return true;
} catch(PDOException $e) {
return false;
}
}

public function close() {
return true;
}

public function destroy($session_id) {
$sth = DBCxn::get()->prepare("DELETE FROM sessions WHERE session_id = ?");
$sth->execute(array($session_id));
return true;
}

public function gc($maxlifetime) {
$sth = DBCxn::get()->prepare("DELETE FROM sessions WHERE session_id = ?");
$sth->execute(array(time() . $maxlifetime));
return true;
}

public function read($session_id) {
$sth = DBCxn::get()->prepare("SELECT session_data FROM sessions WHERE session_id = ?");
$sth->execute(array($session_id));
$rows = $sth->fetchALL(PDO::FETCH_NUM);
if (count($rows) == 0) {
return '';
}
else {
return $rows[0][0];
}
}

public function write($session_id, $session_data) {
$sth = DBCxn::get()->prepare("UPDATE sessions SET session_data = ?, last_update = NOW() WHERE session_id = ?");
$sth->execute(array($session_data, $session_id));
if ($sth->rowCount() == 0) {
$sth = DBCxn::get()->prepare("INSERT INTO sessions (session_id, session_data, last_update) VALUES (?, ?, NOW())");
$sth->execute(array($session_id, $session_data));
}


}
}

session_set_save_handler(new DBHandler);

然后我启动 session ,然后使用开关(通过get 变量或ajax 访问)在index.html 中包含主div 的内容。 PHP.

如果通过 ajax 访问,则开关具有以下验证:

if(!isset($_SESSION)) 
{
session_start();
}

if(isset($_SESSION['l'])) {

在 index.php 结束时, session 通过 session_write_close();

关闭

数据库表对唯一 session ID 有约束。

出于某种原因,只有某些时候我会收到以下错误:

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '312505097fb815255103447952d0af61' for key 'PRIMARY'' in file_path:99 Stack trace: #0 file_path(99): PDOStatement->execute(Array) #1 [internal function]: DBHandler->write('312505097fb8152...', 'login|i:1;') #2 file_path(185): session_write_close() #3 {main} thrown in file_path:99

file_path:99 is the folowing line in the previously mentioned class "$sth->execute(array($session_id, $session_data));"

file_path:185 is session_write_close() at the end of index.php

什么可能导致此错误?

最佳答案

写入函数正在生成重复错误。如果 session 数据与数据库中存储的 session 数据相同,那么它将始终为 $sth->rowCount() 返回值 0。因此它尝试插入具有相同 session_id 的 session 数据并产生重复错误。

关于php - session 处理类奇怪的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35145195/

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