通过$ _SESSION从一个脚本发送到另一个脚本期间的数据丢失 [英] Data loss during sending via $_SESSION from one script to another

查看:46
本文介绍了通过$ _SESSION从一个脚本发送到另一个脚本期间的数据丢失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正尝试通过以下方式将一个充满属性的对象从一个PHP发送到另一个PHP:

I'm trying to send an object full of properties from one PHP to another with:

$_SESSION['object'] = $obj;

其中 $ obj 是用foreach循环指定的对象:

where $obj is an object designated with foreach loop:

foreach($array_of_objects as $obj) {
    $_SESSION['object'] = $obj;
    // here the second PHP file is invoked as a lightbox with tinybox2
    echo "<article onclick=\"TINY.box.show({url:'tinybox2/popup.php',boxid:'popup'})\">Article Content</article>";
}

在第二个PHP文件中,我尝试打印通过的对象:

In second PHP file, I try to print the object passed:

print_r($_SESSION['object']);

以及所有日期格式为Y-m-d(yyyy-mm-dd,例如2016-08-31)的属性都是空的,看起来像这样:

and all the properties that had date in format of Y-m-d (yyyy-mm-dd, eg. 2016-08-31) are just empty, and looks that way:

Card Object (
    [number] => 1234
    [order_number] => ABCDE0000012345
    [name] => Test Name
    [date_composed] => 2016-08-31 14:28
    [companys] => My Company \ 100 %
    [selected_company] => My Company
    [selected_company_part] => 100
    [user] => Test User Name
    [status] => test status 123
    [group] => Dummy
    [coordinator] => Test Coordinator Name
    [order_type] => n/a
    [supplier] => Test Supplier
    [task_completion_date] =>
    [confirmed_completion_date] =>
    [department] => Test Department
    [priority] => 2 - medium
    [created_by] => Test Creator Name
    [cost] => 0
    [external_status] => test status 321
) 

您可以清楚地看到两个属性有问题.它们的正确值应为:

You can clearly see something is wrong with two properties. Their correct values shall be:

(...)[task_completion_date] => 2016-09-10 [confirmed_completion_date] => 2016-09-05(...)

(...) [task_completion_date] => 2016-09-10 [confirmed_completion_date] => 2016-09-05 (...)

为了进行测试,我尝试发送存储对象数组的整个对象:

For testing, I tried out sending the whole object that array of objects is stored in:

$_SESSION['object_with_array'] = $object_with_array;

并在第二个PHP中对其进行了查看,并且...在第一种方法中消失的属性在这里是完整无缺的并且可以正常工作!

and viewed it in the second PHP, and... the properties that were gone in the first approach, here are intact and working!

(...)[task_completion_date] => 2016-09-10 [confirmed_completion_date] => 2016-09-05(...)

(...) [task_completion_date] => 2016-09-10 [confirmed_completion_date] => 2016-09-05 (...)

因此,我只想从数组中发送一个对象,而不是包含数组的整个对象.同样在第一个PHP中查看 $ obj 变量和 $ _ SESSION ['object'] 确实会导致查看存储在两个对象实例中的日期(如上所述),因此在第二个PHP文件中从会话读取过程中发生问题,而不是在第一个PHP文件中进行保存或读取过程中发生问题.

Therefore I want to send only one object from the array, not the whole object that contains the array. Also viewing the $obj variable and $_SESSION['object'] in the first PHP does result in viewing dates stored in both object instances (as shared above), so the problem happens during the reading from the session in the second PHP file, not during saving or reading in the first one.

我的问题是:通过会话将对象发送到第二个文件期间,对象中存储的那些日期发生了什么?为什么仅在发送单个对象而不是包含数组的抽象性更高的对象时发生?较少抽象的对象?如何使过程正常运行?

My question is: what is happening to those dates stored in an object during sending the object via session to the second file and why is it happening only when sending the single object, but not the more abstractive object containing array of less abstractive objects? How to make the process work properly?

仅供参考构建对象的类类似于以下内容:

FYI The class the object is built on looks similar to the below:

class Card
{
    // PROPERTIES
    public $number;
    public $order_number;
    public $name;
    public $date_composed; // FORMAT: yyyy-mm-dd hours:mins; WORKS IN 2'nd PHP
    public $companys;
    public $selected_company;
    public $selected_company_part;
    public $user;
    public $status;
    public $group;
    public $coordinator;
    public $type;
    public $supplier;
    public $task_completion_date; // THIS DATE DISAPEARS
    public $confirmed_completion_date; // THIS DATE DISAPPEARS
    public $department;
    public $priority;
    public $created_by;
    public $cost;
    public $external_status;

    // METHODS
    // Main public constructor
    public function __construct($data) {
        $column = 0;
        foreach($this as $key => $value) {
            $this->$key = $data[$column];
            $column++;
        }
    }

上面提到的对象数组存储在类似于下面的类中:

The array of objects mentioned above is stored in a class similar to below:

class Cards
{
    // PROPERTIES
    public $houseOfCards; // $array_of_objects
    // some other properties that ain't important       

    // METHODS
    // Public constructor reading data from CSV files to objects in an array
    public function __construct() {
        $this->houseOfCards = array();      
        // Here the reading from CSV happens to $data in a loop
            $this->houseOfCards[] = new Card($data);
        // file is being closed after it is read whole
    }
}

非常感谢您的帮助:)

推荐答案

好的,所以我检查了我该怎么做,当我看到它时,我真的...没关系.因此,这就是答案.

Okay, so I checked out what could I do wrong, and when I saw it, I literally... nevermind. Therefore, here's the answer.

从不.曾经.经过.数据.尽管.显示中.东西.

Never. Ever. Pass. Data. While. Displaying. Stuff.

否则,您将一遍又一遍地覆盖您的数据::x

Otherwise you'll overwrite your data over and over... :x

发生的事情是,我数组的最后一个元素没有 $ task_completion_date .最后一个元素是我的第二个PHP看到的元素.为什么?因为每次显示调用元素时都会发送对象,而不是单击时才发送对象.

What happened is that, my last element of array doesn't have $task_completion_date. And that last element was the one that my second PHP saw. Why? Because I was sending the object each time the invoking element was displayed, not clicked.

尽管我真的不知道如何仅将一个对象数据正确发送到第二个脚本,但是在我什至开始显示内容之前,我正在通过会话发送整个 $ array_of_objects 用户可以单击.每个可点击的元素都获得了 tinybox2 发布函数发送的键值.现在,我可以通过以下方式访问数据:

Though I don't really have an idea how to send properly only one object data to the second script, at the moment I'm sending whole $array_of_objects with session before I even start displaying stuff the user can click on. Each clickable element gained the key value sent by tinybox2 post function. Now I can access data through:

$the_object_i_need = $_SESSION['array_of_objects'][$_POST['key']];

或更清楚一点$ array = $ _SESSION ['array_of_objects'];$ key = $ _POST ['key'];$ the_object_i_need = $ array [$ key];

or to be clearer $array = $_SESSION['array_of_objects']; $key = $_POST['key']; $the_object_i_need = $array[$key];

通过键的值通过POST的tinybox2实现发送的事实,只有用户可以看到,并且我不太喜欢它,但是可以肯定的是,这解决了我的问题,我可以在第二步访问适当的对象数据文件.

Only the key value may be visible to the user through the fact it's sent via tinybox2 implementation of POST, and I don't really like it, but for sure this solves my problem and I can access proper object data in the second file.

感谢@nerdlyist的灵感来编写此答案,并感谢@ sumit-badaya再次提出传递整个数组的想法,而我试图忽略这一点.

Thanks for @nerdlyist for inspiration to write this answer and @sumit-badaya for once again suggesting the idea of passing the whole array, which I tried to ommit.

当前的PHP1:

$_SESSION['array_of_objects'] = $array_of_objects;
foreach($array_of_objects as $key=>$obj) {
    // here the second PHP file is invoked as a lightbox with tinybox2
    echo "<article onclick=\"TINY.box.show({url:'tinybox2/popup.php',post:'key=$key',boxid:'popup'})\">Article Content</article>";
}

当前的PHP2:

$array_of_objects = $_SESSION['array_of_objects'];
$key = $_POST['key'];
$requested_object = $array_of_objects[$key];
// Here the data is printed in a form of nice table

这篇关于通过$ _SESSION从一个脚本发送到另一个脚本期间的数据丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆