数据库中的php会话仅将部分信息写入表中
更新(为执行读/写操作的类添加了代码)数据库中的php会话仅将部分信息写入表中,php,session,session-variables,Php,Session,Session Variables,更新(为执行读/写操作的类添加了代码) 查看SerialZeIE()函数,并在写入数据库之前考虑使用它;和相应的unserialize(),当读取时,要访问PHP上的会话数据,您需要在会话开始之前进行。谢谢回复标记。在write()函数中,我在sql语句中将$session_数据替换为serialize($session_data),然后在read()函数中,我将return unserialize($fields[“session_data”])替换为return$fields[“sessi
<代码>
查看SerialZeIE()函数,并在写入数据库之前考虑使用它;和相应的unserialize(),当读取时,要访问PHP上的会话数据,您需要在会话开始之前进行。
谢谢回复标记。在write()函数中,我在sql语句中将$session_数据替换为serialize($session_data),然后在read()函数中,我将return unserialize($fields[“session_data”])替换为return$fields[“session_data”];但还是一样。我把它放错地方了吗?你需要在每个页面上启动会话在类中,每次创建对象时都会实例化一个会话启动。我已经确保所有页面都有一个实例化的对象,但结果仍然相同。在调用header(location…)之前,我在填充会话变量方面遇到了问题。它根本不起作用,我快要发疯了——然后我在PHP帮助页面上找到了这个:试试这个,让我们知道。干杯,谢谢Ronalod,但我没能成功。下面是我得到的信息:“会话已经启动-忽略会话_start()”,当我将会话_start()放在头()之前时调用…这意味着我正在使用的类被正确实例化并开始了会话…我快要发疯了…这一次我真的很沮丧…花了8个月的时间在一个几乎准备好测试版的项目上,现在我甚至无法登录,因为我看不到一些小的更改。我们将不得不更多地了解您用于保存的过程会话数据。您说您正在将其存储在数据库中—会话读/写处理程序是如何设置的?我已使用执行读/写操作的类对象更新了帖子。谢谢你的帮助。<?php
error_reporting(E_ALL);
class dbSession
{
function dbSession($gc_maxlifetime = "", $gc_probability = "", $gc_divisor = "")
{
if ($gc_maxlifetime != "" && is_integer($gc_maxlifetime)) {
@ini_set('session.gc_maxlifetime', $gc_maxlifetime);
}
if ($gc_probability != "" && is_integer($gc_probability)) {
@ini_set('session.gc_probability', $gc_probability);
}
if ($gc_divisor != "" && is_integer($gc_divisor)) {
@ini_set('session.gc_divisor', $gc_divisor);
}
$this->sessionLifetime = ini_get("session.gc_maxlifetime");
session_write_close();
session_set_save_handler(
array(&$this, 'open'),
array(&$this, 'close'),
array(&$this, 'read'),
array(&$this, 'write'),
array(&$this, 'destroy'),
array(&$this, 'gc')
);
register_shutdown_function('session_write_close');
@session_start();
}
function open($save_path, $session_name)
{
$mySQLHost = "localhost";
$mySQLUsername = "username";
$mySQLPassword = "password";
$mySQLDatabase = "rst_sessions";
$link = mysql_connect($mySQLHost, $mySQLUsername, $mySQLPassword);
if (!$link) {
die ("Could not connect to database!");
}
$dbc = mysql_select_db($mySQLDatabase, $link);
if (!$dbc) {
die ("Could not select database!");
}
return true;
}
function close()
{
mysql_close();
return true;
}
function read($session_id)
{
$result = @mysql_query("
SELECT
session_data
FROM
session_data
WHERE
session_id = '".$session_id."' AND
http_user_agent = '".$_SERVER["HTTP_USER_AGENT"]."' AND
session_expire > '".time()."'
");
if (is_resource($result) && @mysql_num_rows($result) > 0) {
// return found data
$fields = @mysql_fetch_assoc($result);
// don't bother with the unserialization - PHP handles this automatically
return $fields["session_data"];
}
return "";
}
function write($session_id, $session_data)
{
// first checks if there is a session with this id
$result = @mysql_query(" SELECT *FROM session_data WHERE session_id = '".$session_id."'");
if (@mysql_num_rows($result) > 0)
{
$result = @mysql_query(" UPDATE session_data
SET
session_data = '".$session_data."',
session_expire = '".(time() + $this->sessionLifetime)."',
account_id = '" . $_SESSION['account']['account_id'] . "',
username = '" . $_SESSION['users']['username'] . "',
report_logo_path = '". $_SESSION['path_to_report_logo'] . '/' . $_SESSION['report_logo_img'] . "',
report_footer_all = '". $_SESSION['report_footer_all'] . "',
report_footer_summary= '". $_SESSION['report_footer_summary'] . "'
WHERE
session_id = '".$session_id."'
");
// if anything happened
if (@mysql_affected_rows())
{
return true;
}
}
else // if this session id is not in the database
{
$sql = "
INSERT INTO
session_data
(
session_id,
http_user_agent,
session_data,
session_expire,
account_id,
username
)
VALUES
(
'".serialize($session_id)."',
'".$_SERVER["HTTP_USER_AGENT"]."',
'".$session_data."',
'".(time() + $this->sessionLifetime)."',
'".$_SESSION['account']['account_id']."',
'".$_SESSION['users']['username']."'
)
";
$result = @mysql_query($sql);
if (@mysql_affected_rows())
{
// return an empty string
return "";
}
}
// if something went wrong, return false
return false;
}
}
?>