[英]PHP session not set the first time
I have an odd issue. 我有一个奇怪的问题。 The first time a visitor comes to the site and I set anything is the session, it doesn't stick. 访客第一次来到该站点,我设置的是会话,它不会停留。 The second and all the following times I try to set something it sticks. 第二次以及以后的所有时间,我都会尝试设置一些固定的东西。 After the initial try I can destroy the session and set something and it sticks. 初步尝试后,我可以销毁该会话并设置一些东西,并且它会粘住。 Its just the initial attempt to save something fails. 它只是最初尝试保存某些东西而失败。 I'm trying to save something to the session with $_SESSION['uid'] = $row["Id"];
我正在尝试使用$_SESSION['uid'] = $row["Id"];
将某些内容保存到会话中 . 。 I know the $row["Id"]
is valid and holds data (I echoed it). 我知道$row["Id"]
是有效的并保存数据(我已将其回显)。
I am not using standard sessions. 我没有使用标准会话。 I am saving the session into a database. 我将会话保存到数据库中。 My session class is below. 我的课程在下面。 Is there anything I'm missing or doing wrong to explain this behavior? 我有什么想念或做错的事情来解释这种行为吗?
Update : 更新 :
Well I tested the session class on its own and it seems to be working :-/ But when I use it in my larger application _write
never gets called, though __destruct
does get called. 好吧,我自己测试了会话类,它似乎正在工作:-/但是,当我在较大的应用程序中使用它时,虽然会调用__destruct
,但是_write
不会被调用。 Any idea why that may be? 知道为什么会这样吗?
<?php
include_once('db.php');
class PDOSession
{
protected $pdo;
protected $table = 'SessionData';
public function __construct()
{
// Get a database connection
$db = new PDOConnectionFactory();
$this->pdo = $db->getConnection(true);
// Start session
session_set_save_handler(array($this, '_open'),
array($this, '_close'),
array($this, '_read'),
array($this, '_write'),
array($this, '_destroy'),
array($this, '_gc'));
session_start();
}
public function __destruct()
{
session_write_close();
}
protected function fetchSession($id)
{
$stmt = $this->pdo->prepare('SELECT id, data FROM '.$this->table.' WHERE id = :id AND unixtime > :unixtime');
$stmt->execute(array(':id' => $id, ':unixtime' => (time() - (int)ini_get('session.gc_maxlifetime'))));
$sessions = $stmt->fetchAll();
return empty($sessions) ? false : $sessions[0];
}
public function _open($savePath, $sessionName)
{
return true;
}
public function _close()
{
return true;
}
public function _read($id)
{
$session = $this->fetchSession($id);
return ($session === false) ? false : $session['data'];
}
public function _write($id, $sessionData)
{
$session = $this->fetchSession($id);
if($session === false) {
$stmt = $this->pdo->prepare('INSERT INTO '.$this->table.' (id, data, unixtime) VALUES (:id, :data, :time)');
} else {
$stmt = $this->pdo->prepare('UPDATE '.$this->table.' SET data = :data, unixtime = :time WHERE id = :id');
}
$stmt->execute(array(
':id' => $id,
':data' => $sessionData,
':time' => time()
));
}
public function _destroy($id)
{
$stmt = $this->pdo->prepare('DELETE FROM '.$this->table.' WHERE id = :id');
$stmt->execute(array(':id' => $id));
}
public function _gc($maxlifetime)
{
$stmt = $this->pdo->prepare('DELETE FROM '.$this->table.' WHERE unixtime < :time');
$stmt->execute(array(':time' => (time() - (int) $maxlifetime)));
}
}
$newPDOSessionStartHere = new PDOSession();
I'm a bit of an idiot I guess. 我猜我有点白痴。 I was calling session_destroy()
rather than session_unset()
to clear things out at the top of my authentication script. 我正在调用session_destroy()
而不是session_unset()
来清除身份验证脚本顶部的内容。 The class works fine. 这堂课很好。
I think that you should start a session after you define your class. 我认为您应该在定义课程后开始一个会话。 Not inside. 不在里面
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.