- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
好吧,我很困惑。我有一个存储在 session 中的对象。我可以向这个对象添加项目。到目前为止很简单。我这样初始化对象:
$template = new Template($mysqli);
$_SESSION['template'] = serialize($template);
现在应该创建一个全新的品牌对象并将其分配给 session 。然后我有一些代码通过 AJAX 请求添加项目。该代码如下:
$template = unserialize($_SESSION['template']);
$prodid = $_GET['product_id'];
$template->addItem($prodid);
echo var_dump($template->getItems());
$_SESSION['template'] = serialize($template);
同样,应该很简单。现在的问题是,第一段代码没有重置 $_SESSION['template']
而它应该重置,所以我得到了到目前为止添加的所有项目,重新加载页面没有修复它。
我找到了导致问题的文件,但我不知道我能做些什么。它是一个包含项,网站的不同部分都需要它才能正常运行。我正在向网站添加功能,如果我删除功能,我认为所有者不会满意。这是文件:
<?php
include_once( 'DBE.class.php' ) ;
################################################
# Function: Sessions_open
# Parameters: $path (string), $name (string)
# Returns: bool
# Description: This is an over-ride function call
# that we need to create so that the php internal
# session manager doesn't store our session in the
# file system, since we are storing it in the
# db. Storing a session in a file system on the
# server inhibits scalability for two reasons:
# 1: A large number of users may be hitting the site
# and clog the space on the hard-drive of the server
# due to the sheer amount of session files stored
# 2: The website may be behind a load-balancer and
# therefore the server handling the page request
# may not have the session stored on its file system
################################################
function Sessions_open ( $path, $name ) {
return TRUE ;
}
################################################
# Function: Sessions_close
# Parameters: N/A
# Returns: bool
# Description: This is an over-ride function call
# that we need to create so that the php internal
# session manager doesn't store our session in the
# file system, since we are storing it in the
# db. Storing a session in a file system on the
# server inhibits scalability for two reasons:
# 1: A large number of users may be hitting the site
# and clog the space on the hard-drive of the server
# due to the sheer amount of session files stored
# 2: The website may be behind a load-balancer and
# therefore the server handling the page request
# may not have the session stored on its file system
################################################
function Sessions_close () {
return TRUE ;
}
################################################
# Function: Sessions_read
# Parameters: $SessionID (string)
# Returns: (string) or (false) on error
# Description: This function is used at startup to read
# the contents of the session.
# If no sess data, the empty string ("") is returned.
# Otherwise, the serialized sess data is returned.
# On error, false is returned.
################################################
function Sessions_read ( $SessionID ) {
include_once( 'DBE.class.php' ) ;
$dbe = new DBE() ;
//default return value to false
$returnVal = FALSE ;
$query = "SELECT DataValue
FROM Sessions
WHERE SessionID = '$SessionID' " ;
$result = $dbe->Select( $query ) ;
if( count( $result ) == 1 ) {
$returnVal = $result[0]['DataValue'] ;
//update the session so that we don't time-out after creating
$query = "UPDATE Sessions
SET LastUpdated = NOW()
WHERE SessionID = '$SessionID'" ;
$dbe->Update( $query ) ;
} else {
//Insert here to simplify the write function
$query = "INSERT INTO Sessions (SessionID, DataValue) VALUES ( '$SessionID', '' )" ;
$dbe->Insert( $query ) ; //pass the insert stmt
//set returnVal to '' being that we didn't find the SessionID
$returnVal = '' ;
}
return( $returnVal ) ;
}
################################################
# Function: Sessions_write
# Parameters: $SessionID (string), $Data
# Returns: bool
# Description: This function is used at startup to read
# the contents of the session.
# If no sess data, the empty string ("") is returned.
# Otherwise, the serialized sess data is returned.
# On error, false is returned.
################################################
function Sessions_write( $SessionID, $Data ) {
include_once( 'DBE.class.php' ) ;
$dbe = new DBE() ;
//default to true
$returnVal = TRUE ;
//update the session
$query = "UPDATE Sessions
SET DataValue = '$Data'
WHERE SessionID = '$SessionID'" ;
$result = $dbe->Update( $query ) ; //pass the update stmt to the dbEngine..
//test for success
if( $result == -1 )
$returnVal = FALSE ;
//return the return value
return( $returnVal ) ;
}
################################################
# Function: Sessions_delete
# Parameters: $SessionID (string)
# Returns: bool
# Description: This function is used to delete the session
################################################
function Sessions_destroy( $SessionID ) {
include_once( 'DBE.class.php' ) ;
$dbe = new DBE() ;
$query = "DELETE FROM Sessions WHERE SessionID = '$SessionID' " ;
$dbe->Delete( $query ) ;
return( TRUE ) ;
}
################################################
# Function: Sessions_delete
# Parameters: $SessionID (string)
# Returns: bool
# Description: This function is used to delete the session
################################################
function Sessions_gc( $aMaxLifetime ) {
include_once( 'DBE.class.php' ) ;
$dbe = new DBE() ;
$query = "DELETE FROM Sessions WHERE (UNIX_TIMESTAMP(NOW()) - UNIX_TIMESTAMP( LastUpdated )) > $aMaxLifetime " ;
$dbe->Delete( $query ) ;
return( TRUE ) ;
}
session_set_save_handler( "Sessions_open", "Sessions_close",
"Sessions_read", "Sessions_write",
"Sessions_destroy", "Sessions_gc" ) ;
?>
我认为这正在改变 session 的基本功能,但我不太确定。这导致我在 session 中重置模板时遇到麻烦。任何人都有任何想法或知道我能做些什么来解决这个问题。我完全被难住了,所以非常感谢任何帮助。
最佳答案
我不确定这是否是问题所在,但这是我阅读您的代码时跳出的内容:
您的序列化对象依赖于 mysql 连接
$template = new Template($mysqli);
虽然您的对象(也许)可以毫无问题地序列化和反序列化,但 mysql 连接不能,因此您的未序列化 $template 会尝试对无效的连接/文件句柄进行操作。
您可以尝试将未序列化的对象重新附加到有效的数据库连接。
不知道您的模板类中有什么(以及它使用什么资源以及如何使用),很难猜出哪里出了问题,但我希望这是一个足够好的线索,让我们知道从哪里开始寻找。
为了让您更好地理解我在说什么,请考虑以下内容:
<?php
class Template {
function __construct($c) {
$this->conn = $c;
$this->foo = "bar";
}
function get_data() {
$result = mysql_query("select 1234 as test", $this->conn);
$data = mysql_fetch_array($result);
return $data;
}
function attach_db($c) {
$this->conn = $c;
}
}
?>
<?php
session_start();
require('template.php');
$conn = mysql_connect('localhost', 'root', '');
$template = new Template($conn);
?>
<pre>
Your $template var, freshly created:
<?php var_dump($template); ?>
Accessing the resources:
<?php var_dump($template->get_data()); ?>
<?php
$_SESSION['template'] = serialize($template);
?>
</pre>
<?php
session_start();
require('template.php');
$template = unserialize($_SESSION['template']);
?>
<pre>
Unserialized $template:
<?php var_dump($template); ?>
(notice that $template->foo === "bar" so your session un/serialization is working correctly)
Accessing the (now invalid) mysql resources:
<?php var_dump($template->get_data()); ?>
</pre>
调用 first.php 应该给你这个:
Your $template var, freshly created:
object(Template)#1 (2) {
["conn"]=>
resource(3) of type (mysql link)
["foo"]=>
string(3) "bar"
}Accessing the resources:
array(2) {
[0]=>
string(4) "1234"
["test"]=>
string(4) "1234"
}
调用 others.php 应该导致:
Unserialized $template:
object(Template)#1 (2) {
["conn"]=>
int(0)
["foo"]=>
string(3) "bar"
}
(notice that $template->foo === "bar" so your session un/serialization is working correctly)Accessing the (now invalid) mysql resources:
Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in template.php on line 9
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in template.php on line 10
bool(false)
要解决这个问题,您可以重新创建无法反序列化的资源。
像这样:
<?php
session_start();
require('template.php');
$template = unserialize($_SESSION['template']);
?>
<pre>
Unserialized $template:
<?php var_dump($template); ?>
Attaching a valid db connection:
<?php
$conn = mysql_connect('localhost', 'root', '');
$template->attach_db($conn);
var_dump($template);
?>
Accessing the resources:
<?php var_dump($template->get_data()); ?>
</pre>
现在,在调用 first.php 之后调用 solution.php 应该会得到:
Unserialized $template:
object(Template)#1 (2) {
["conn"]=>
int(0)
["foo"]=>
string(3) "bar"
}Attaching a valid db connection:
object(Template)#1 (2) {
["conn"]=>
resource(3) of type (mysql link)
["foo"]=>
string(3) "bar"
}Accessing the resources:
array(2) {
[0]=>
string(4) "1234"
["test"]=>
string(4) "1234"
}
正如我所说,在不知道您的模板类做什么的情况下,无法确定发生了什么。这只是一种可能性;)
祝你好运!
关于PHP session 困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1038830/
我有一个程序可以打开一个窗口并快速改变背景颜色并随机弹出矩形和椭圆形。我的代码有效,但我不知道为什么,因为我没有在我的代码中调用 repaint() 函数。当我使用我个人的 update() 函数包含
var allRapidSpells = $$('input[value^=RSW]'); 谁能告诉我这是做什么的? 最佳答案 我敢猜测您正在使用 MooTools ,一个 JavaScript 框架
我有一个抽象父类,它有多个子类。我希望 child 能够拥有一个对于该 child 的每个实例都相同的变量。我不想将构造函数传递给 child 来告诉它它的名字,因为当它可以被硬编码时,这看起来很愚蠢
我刚刚在 Git 存储库上做了一些糟糕的事情,我不知道如何解决这个问题。我什至不知道我是怎么把它弄成这样的……! 在存储库(托管在 git hub 上)上,有 3 个我感兴趣的分支:master、br
我是 GIT 的新手,在理解提交日志图时遇到问题。 我感觉每条平行线都是一个分支。虽然我的源代码只有 2 个分支。我在下面提供的提交日志图中看到 3-4 条平行线(Microsoft Team Ser
我是 WPF 的新手,ScrollViewer 让我很沮丧。要么我只是没有“得到”它,要么它是一种有限的控制。 这是我的挫折: 水平滚动错误 水平滚动条仅在列表底部可见(我必须滚动到底部才能看到) 坏
那么 $('table.selectable td.capable input:text') 比 $('table.selectable td input:text') 更好吗?换句话说,指定一个类会
我刚刚完成了计算机图形学类(class),我们必须对光线追踪器进行编程。尽管所有结果都是正确的,但我对 OpenMP 的使用感到困惑(顺便说一句,这不是类(class)的一部分)。我有这个循环(C++
与 PatternSynonyms ( explicitly bidirectional form ),pattern-to-expr 方程实际上形成了一个函数,但拼写为大写(假设您最终得到正确类型的
我是 javascript/coffeescript 新手。 有人可以解释一下为什么这个 CoffeeScript/JavaScript 会毫无延迟地快速通过吗?我对第一种情况的想法是,它是对 upd
如果我调用document.getElementsByClassName('cl'),我会得到一个 HTMLCollection。它似乎包含 Element 对象而不是 HTMLElement 对象,
这是我本月的 azure payasyougo 使用费用。 我很难理解为什么我要为标准中型应用服务付费,我认为它会包含在计算时间中?我只运行一个云服务,这对于一个没有做太多事情的云服务来说似乎有点陡峭
除了the issue I am already having之外,我还在I saw a video on it之后安装了HBase(尚未安装)之前,还安装了Zookeeper。在安装它时,我遇到了许
我正在将 XSLT 与 regexp:match exslt 函数一起使用。上述函数采用 JavaScript Regex 模式。因此,我尝试匹配一组数字 1 到 3 OR 5 到 7 OR 9 到
我想知道为什么这段代码会给出消息:SyntaxError:意外的标记其他。 var compare = function(choice1,choice2){ if(choice1===choice2)
我尝试使用复选框和 JQuery 过滤日历上的事件, $(document).ready(function () { $('.scrollable-menu :checkbox').click(f
假设我们有一个用户想要一个名为:“test/lasdhjal.txt”,无论如何。现在,如果我将其放入新的文件(输入)中;对象里面,它会认为 test/是一个文件夹,而它是名称的一部分。我能做什么呢?
问题是 stash 的更改不会留在我 stash 它们的分支中。其他分支存储将被覆盖示例: 我愿意: git checkout iss4 // made some changes gi
我是一个 java 新手,并且在 StackOverflow 错误/在类之间访问文件的能力方面遇到了一个非常令人困惑的问题。我知道根本原因可能是我进行了一些递归调用,但修复它的语法却让我无法理解。我认
public X createData(int n) { int[] values = new int[n]; Random rand = new Random(); for
我是一名优秀的程序员,十分优秀!