AJAX 操作期间 PHP 会话中的类对象重置?

发布于 2024-11-14 10:21:03 字数 2285 浏览 3 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你的笑 2024-11-21 10:21:03

您需要在remote_ajax 文件中包含该类(在session_start() 之前):

编辑:序列化/反序列化要求是PHP4 的限制。

请求文件:

<?php
include('ajaxtest_class.php');
session_start();
$_SESSION['obj'] = serialize(new TestClass('foo,bar'));

远程文件:

<?php
    session_start();
    include('ajaxtest_class.php');
    $obj = unserialize($_SESSION['obj']);
    echo 'count=' . count($obj->dataList);
?>

在 PHP 中,类构造函数应该以不同的方式定义:

<?php
    class TestClass {
        var $dataList;
        function __construct($incoming) {
            $this->dataList = explode(',',$incoming);
        }
    }
?>

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:

<?php
include('ajaxtest_class.php');
session_start();
$_SESSION['obj'] = serialize(new TestClass('foo,bar'));

Remote file:

<?php
    session_start();
    include('ajaxtest_class.php');
    $obj = unserialize($_SESSION['obj']);
    echo 'count=' . count($obj->dataList);
?>

In PHP, the class constructor should be defined differently:

<?php
    class TestClass {
        var $dataList;
        function __construct($incoming) {
            $this->dataList = explode(',',$incoming);
        }
    }
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文