AJAX 操作期间 PHP 会话中的类对象重置?
我有一个 PHP 会话和 AJAX 的简单示例,它在会话中保存数组时起作用:
请求文件:
<?php
session_start();
$_SESSION['data'] = array('foo','bar');
echo count($_SESSION['data']);
?>
<html>
<head>
<title>Test</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
</head>
<body>
<button id="but1">Go</button>
<script type="text/javascript">
$('#but1').click(function() {
$.ajax({
url:'ajaxtest_remote.php',
success:function(result) {
alert(result);
}
});
});
</script>
</body>
</html>
远程文件:
<?php
session_start();
echo 'count=' . count($_SESSION['data']);
?>
第一个文件上的回显显示 2,成功函数中的警报显示“count=2”。快乐的日子。
问题发生的地方是,如果我将数组交换为类对象:
请求文件:
<?php
session_start();
include('ajaxtest_class.php');
$_SESSION['obj'] = new TestClass('foo,bar');
echo count($_SESSION['obj']->dataList);
?>
<!-- HMTL AS ABOVE -->
远程文件:
<?php
session_start();
echo 'count=' . count($_SESSION['obj']->dataList);
?>
类文件:
<?php
class TestClass {
var $dataList;
function TestClass($incoming) {
$this->dataList = explode(',',$incoming);
}
}
?>
这仍然在第一页上显示 2,但 ajax 成功警报返回“count=0”。谁能解释这是为什么吗?
更新1
如果我将类文件导入到远程,它仍然不起作用,尽管我可以证明该类已加载。
<?php
session_start();
include('ajaxtest_class.php');
$c = new TestClass('a,b,c');
echo 'count=' . count($_SESSION['obj']->dataList) . '-' . count($c->dataList);
?>
ajax 成功后的新警报显示为 count=0-3
。
更新2
var_dump($_SESSION['obj']);
object(__PHP_Incomplete_Class)#8 (2) {
["__PHP_Incomplete_Class_Name"]=>
string(9) "TestClass"
["dataList"]=>
array(2) {
[0]=>
string(3) "foo"
[1]=>
string(3) "bar"
}
}
I have a simple example of PHP sessions and AJAX, which works when holding an array in session:
Request file:
<?php
session_start();
$_SESSION['data'] = array('foo','bar');
echo count($_SESSION['data']);
?>
<html>
<head>
<title>Test</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
</head>
<body>
<button id="but1">Go</button>
<script type="text/javascript">
$('#but1').click(function() {
$.ajax({
url:'ajaxtest_remote.php',
success:function(result) {
alert(result);
}
});
});
</script>
</body>
</html>
Remote file:
<?php
session_start();
echo 'count=' . count($_SESSION['data']);
?>
The echo on the first file shows 2, and the alert in the success function displays "count=2". Happy days.
Where the problem happens is if I swap my array for a class object:
Request File:
<?php
session_start();
include('ajaxtest_class.php');
$_SESSION['obj'] = new TestClass('foo,bar');
echo count($_SESSION['obj']->dataList);
?>
<!-- HMTL AS ABOVE -->
Remote File:
<?php
session_start();
echo 'count=' . count($_SESSION['obj']->dataList);
?>
Class File:
<?php
class TestClass {
var $dataList;
function TestClass($incoming) {
$this->dataList = explode(',',$incoming);
}
}
?>
This still displays a 2 on the first page, but the ajax success alert comes back "count=0". Can anyone explain why this is?
Update1
If I import the class file into remote it still doesn't work, although I can prove the class is loaded.
<?php
session_start();
include('ajaxtest_class.php');
$c = new TestClass('a,b,c');
echo 'count=' . count($_SESSION['obj']->dataList) . '-' . count($c->dataList);
?>
The new alert from the ajax success reads count=0-3
.
Update2
var_dump($_SESSION['obj']);
object(__PHP_Incomplete_Class)#8 (2) {
["__PHP_Incomplete_Class_Name"]=>
string(9) "TestClass"
["dataList"]=>
array(2) {
[0]=>
string(3) "foo"
[1]=>
string(3) "bar"
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要在remote_ajax 文件中包含该类(在
session_start()
之前):编辑:序列化/反序列化要求是PHP4 的限制。
请求文件:
远程文件:
在 PHP 中,类构造函数应该以不同的方式定义:
You would need to include the Class in the remote_ajax file (before
session_start()
):edit: The serialize/unserialize requirement is a limitation of PHP4.
Request file:
Remote file:
In PHP, the class constructor should be defined differently: