好吧,我很困惑.我有一个存储在会话中的对象.我可以添加项目到这个对象.到目前为止很简单.我像这样初始化对象:
$template = new Template($MysqLi);
$_SESSION['template'] = serialize($template);
现在,这应该创建一个品牌打击新对象并将其分配给会话.然后我有一些代码通过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" ) ;
?>
我认为这会改变会话的基本功能,但我不太确定.这导致我在会话中重置模板时遇到麻烦.任何人都有任何想法或知道我可以做些什么来解决这个问题.我完全被难过所以非常感谢任何帮助.
解决方法:
我不确定这是不是问题,但是当我阅读你的代码时,这就是跳出来的:
您的序列化对象依赖于MysqL连接
$template = new Template($MysqLi);
虽然您的对象(可能)可以序列化和未序列化而没有问题,但MysqL连接不能,因此您的未序列化$模板尝试在无效的连接/文件句柄上运行.
您可以尝试将未序列化的对象重新附加到有效的数据库连接.
不知道你的模板类里面有什么(以及它使用了什么资源以及如何使用),很难猜出什么是错的,但我希望这是一个很好的线索,从哪里开始寻找.
为了让您更好地了解我在说什么,请考虑以下事项:
的template.PHP
<?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>
other.PHP
<?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>
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”
}
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)
要解决此问题,您可以重新创建无法取消/序列化的资源.
像这样:
solution.PHP
<?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”
}
正如我所说的,不知道你的模板类是做什么的,不可能肯定地说出发生了什么……这只是一种可能性;)
祝好运!