php-保存在会话中,一系列具有私有属性的对象,然后收集它们

发布于 2025-02-03 21:29:13 字数 7227 浏览 1 评论 0原文

我有一个月的班级对象。我继续进行会议,因此我不必不断检索信息。

看起来像这样的月份类:

<?php

class Month{

    private static $mes;
    private static $year;

    static function void(){
        return new self(null, null);
    }

    function __construct($mes, $year){
        $this->mes = $mes;
        $this->year = $year;
    }

    // GETTERS 
    public function getMes(){
        return $this->mes;
    }

    public function getYear(){
        return $this->year;
    }

    //SETTERS

    public function setMes($mes){
        $this->mes = $mes;
    }

   public function setYear($year){
        $this->year = $year;
    }

    //Métodos
    public function toString(){
        switch ($this->mes) {
            case 1:
                return "Enero - ".$this->year;
                break;
            case 2:
                return "Febrero - ".$this->year;
                break;
            case 3:
                return "Marzo - ".$this->year;
                break;
            case 4:
                return "Abril - ".$this->year;
                break;
            case 5:
                return "Mayo - ".$this->year;
                break;
            case 6:
                return "Junio - ".$this->year;
                break;
            case 7:
                return "Julio - ".$this->year;
                break;
            case 8:
                return "Agosto - ".$this->year;
                break;
            case 9:
                return "Septiembre - ".$this->year;
                break;
            case 10:
                return "Octubre - ".$this->year;
                break;
            case 11:
                return "Noviembre - ".$this->year;
                break;
            case 12:
                return "Diciembre - ".$this->year;
                break;
        }
    }
}

然后在显示信息的文件中,我有以下代码:

require_once '../Model/ClassEmpleado.php';
require_once '../Model/ClassMonth.php';
include '../Control/CListado-Leads.php';

if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

if(!isset($_SESSION['meses'])){
    $_SESSION['meses'] = getMonths();
    $meses = $_SESSION['meses'];
    var_dump($_SESSION['meses']);
}
else{
    $meses = $_SESSION['meses'];
    var_dump($_SESSION['meses']);
}

最后,在下面,我显示了一个选项,并在数组中为每个对象提供一个选项。

    <div class="mt-5 container-fluid">
        <form action='#' method='POST'>
            Mes: <select id='filtromes' name='filtromes' class="filtro-mes">
                <option class='opciones-filtro-mes' value='0'>Histórico Completo</option>
                
                <?php 
                    for($i = 0; $i < count($meses); $i++){
                        $mes = $meses[$i];
                        echo "<option class='opciones-filtro-mes' value='" . $mes->getMes() . "-" . $mes->getYear() . "'>".$meses[$i]->toString() . "</option>";
                    }
                ?>
            </select>
        </form>
    </div>

所有这些都可以正常工作,第一次访问文件时,我收集信息并将其存储在会话中。

问题稍后,重新加载页面或从其他页面再次访问它时,会话中的数组似乎已经更改并且不再起作用。

如果我将会话变量var_dump var_dump,我将获得此输出:

第一次:

array(15) { [0]=> object(Month)#4 (2) { ["mes"]=> string(1) "4" ["year"]=> string(4) "2021" } [1]=> object(Month)#5 (2) { ["mes"]=> string(1) "5" ["year"]=> string(4) "2021" } [2]=> object(Month)#6 (2) { ["mes"]=> string(1) "6" ["year"]=> string(4) "2021" } [3]=> object(Month)#7 (2) { ["mes"]=> string(1) "7" ["year"]=> string(4) "2021" } [4]=> object(Month)#8 (2) { ["mes"]=> string(1) "8" ["year"]=> string(4) "2021" } [5]=> object(Month)#9 (2) { ["mes"]=> string(1) "9" ["year"]=> string(4) "2021" } [6]=> object(Month)#10 (2) { ["mes"]=> string(2) "10" ["year"]=> string(4) "2021" } [7]=> object(Month)#11 (2) { ["mes"]=> string(2) "11" ["year"]=> string(4) "2021" } [8]=> object(Month)#12 (2) { ["mes"]=> string(2) "12" ["year"]=> string(4) "2021" } [9]=> object(Month)#13 (2) { ["mes"]=> string(1) "1" ["year"]=> string(4) "2022" } [10]=> object(Month)#14 (2) { ["mes"]=> string(1) "2" ["year"]=> string(4) "2022" } [11]=> object(Month)#15 (2) { ["mes"]=> string(1) "3" ["year"]=> string(4) "2022" } [12]=> object(Month)#16 (2) { ["mes"]=> string(1) "4" ["year"]=> string(4) "2022" } [13]=> object(Month)#17 (2) { ["mes"]=> string(1) "5" ["year"]=> string(4) "2022" } [14]=> object(Month)#18 (2) { ["mes"]=> string(1) "6" ["year"]=> string(4) "2022" } }

从另一页重新加载或访问时:

array(15) { [0]=> object(Month)#2 (2) { ["mes":"Month":private]=> string(1) "4" ["year":"Month":private]=> string(4) "2021" } [1]=> object(Month)#3 (2) { ["mes":"Month":private]=> string(1) "5" ["year":"Month":private]=> string(4) "2021" } [2]=> object(Month)#4 (2) { ["mes":"Month":private]=> string(1) "6" ["year":"Month":private]=> string(4) "2021" } [3]=> object(Month)#5 (2) { ["mes":"Month":private]=> string(1) "7" ["year":"Month":private]=> string(4) "2021" } [4]=> object(Month)#6 (2) { ["mes":"Month":private]=> string(1) "8" ["year":"Month":private]=> string(4) "2021" } [5]=> object(Month)#7 (2) { ["mes":"Month":private]=> string(1) "9" ["year":"Month":private]=> string(4) "2021" } [6]=> object(Month)#8 (2) { ["mes":"Month":private]=> string(2) "10" ["year":"Month":private]=> string(4) "2021" } [7]=> object(Month)#9 (2) { ["mes":"Month":private]=> string(2) "11" ["year":"Month":private]=> string(4) "2021" } [8]=> object(Month)#10 (2) { ["mes":"Month":private]=> string(2) "12" ["year":"Month":private]=> string(4) "2021" } [9]=> object(Month)#11 (2) { ["mes":"Month":private]=> string(1) "1" ["year":"Month":private]=> string(4) "2022" } [10]=> object(Month)#12 (2) { ["mes":"Month":private]=> string(1) "2" ["year":"Month":private]=> string(4) "2022" } [11]=> object(Month)#13 (2) { ["mes":"Month":private]=> string(1) "3" ["year":"Month":private]=> string(4) "2022" } [12]=> object(Month)#14 (2) { ["mes":"Month":private]=> string(1) "4" ["year":"Month":private]=> string(4) "2022" } [13]=> object(Month)#15 (2) { ["mes":"Month":private]=> string(1) "5" ["year":"Month":private]=> string(4) "2022" } [14]=> object(Month)#16 (2) { ["mes":"Month":private]=> string(1) "6" ["year":"Month":private]=> string(4) "2022" } }

您可以看到会话中的变量已更改。看起来该变量已被序列化或类似的东西。

如果我将班级的属性视为公开,则可以很好地工作。但是对于安全,我希望他们成为私人。

I have an array of Month class objects. Which I keep in session so I don't have to constantly retrieve the information.

The Month class looks like this:

<?php

class Month{

    private static $mes;
    private static $year;

    static function void(){
        return new self(null, null);
    }

    function __construct($mes, $year){
        $this->mes = $mes;
        $this->year = $year;
    }

    // GETTERS 
    public function getMes(){
        return $this->mes;
    }

    public function getYear(){
        return $this->year;
    }

    //SETTERS

    public function setMes($mes){
        $this->mes = $mes;
    }

   public function setYear($year){
        $this->year = $year;
    }

    //Métodos
    public function toString(){
        switch ($this->mes) {
            case 1:
                return "Enero - ".$this->year;
                break;
            case 2:
                return "Febrero - ".$this->year;
                break;
            case 3:
                return "Marzo - ".$this->year;
                break;
            case 4:
                return "Abril - ".$this->year;
                break;
            case 5:
                return "Mayo - ".$this->year;
                break;
            case 6:
                return "Junio - ".$this->year;
                break;
            case 7:
                return "Julio - ".$this->year;
                break;
            case 8:
                return "Agosto - ".$this->year;
                break;
            case 9:
                return "Septiembre - ".$this->year;
                break;
            case 10:
                return "Octubre - ".$this->year;
                break;
            case 11:
                return "Noviembre - ".$this->year;
                break;
            case 12:
                return "Diciembre - ".$this->year;
                break;
        }
    }
}

Then in the file that shows the information, I have the following code:

require_once '../Model/ClassEmpleado.php';
require_once '../Model/ClassMonth.php';
include '../Control/CListado-Leads.php';

if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

if(!isset($_SESSION['meses'])){
    $_SESSION['meses'] = getMonths();
    $meses = $_SESSION['meses'];
    var_dump($_SESSION['meses']);
}
else{
    $meses = $_SESSION['meses'];
    var_dump($_SESSION['meses']);
}

And finally, below, I show a select with an option for each object in the array.

    <div class="mt-5 container-fluid">
        <form action='#' method='POST'>
            Mes: <select id='filtromes' name='filtromes' class="filtro-mes">
                <option class='opciones-filtro-mes' value='0'>Histórico Completo</option>
                
                <?php 
                    for($i = 0; $i < count($meses); $i++){
                        $mes = $meses[$i];
                        echo "<option class='opciones-filtro-mes' value='" . $mes->getMes() . "-" . $mes->getYear() . "'>".$meses[$i]->toString() . "</option>";
                    }
                ?>
            </select>
        </form>
    </div>

All this works fine, the first time I access the file, I collect the information and store it in session.

enter image description here

The problem comes later, when reloading the page, or accessing it again from a different page, the array in session seems to have changed and no longer works.

enter image description here

If I var_dump the session variables, I get this output:

The first time:

array(15) { [0]=> object(Month)#4 (2) { ["mes"]=> string(1) "4" ["year"]=> string(4) "2021" } [1]=> object(Month)#5 (2) { ["mes"]=> string(1) "5" ["year"]=> string(4) "2021" } [2]=> object(Month)#6 (2) { ["mes"]=> string(1) "6" ["year"]=> string(4) "2021" } [3]=> object(Month)#7 (2) { ["mes"]=> string(1) "7" ["year"]=> string(4) "2021" } [4]=> object(Month)#8 (2) { ["mes"]=> string(1) "8" ["year"]=> string(4) "2021" } [5]=> object(Month)#9 (2) { ["mes"]=> string(1) "9" ["year"]=> string(4) "2021" } [6]=> object(Month)#10 (2) { ["mes"]=> string(2) "10" ["year"]=> string(4) "2021" } [7]=> object(Month)#11 (2) { ["mes"]=> string(2) "11" ["year"]=> string(4) "2021" } [8]=> object(Month)#12 (2) { ["mes"]=> string(2) "12" ["year"]=> string(4) "2021" } [9]=> object(Month)#13 (2) { ["mes"]=> string(1) "1" ["year"]=> string(4) "2022" } [10]=> object(Month)#14 (2) { ["mes"]=> string(1) "2" ["year"]=> string(4) "2022" } [11]=> object(Month)#15 (2) { ["mes"]=> string(1) "3" ["year"]=> string(4) "2022" } [12]=> object(Month)#16 (2) { ["mes"]=> string(1) "4" ["year"]=> string(4) "2022" } [13]=> object(Month)#17 (2) { ["mes"]=> string(1) "5" ["year"]=> string(4) "2022" } [14]=> object(Month)#18 (2) { ["mes"]=> string(1) "6" ["year"]=> string(4) "2022" } }

When reloading or accessing from another page:

array(15) { [0]=> object(Month)#2 (2) { ["mes":"Month":private]=> string(1) "4" ["year":"Month":private]=> string(4) "2021" } [1]=> object(Month)#3 (2) { ["mes":"Month":private]=> string(1) "5" ["year":"Month":private]=> string(4) "2021" } [2]=> object(Month)#4 (2) { ["mes":"Month":private]=> string(1) "6" ["year":"Month":private]=> string(4) "2021" } [3]=> object(Month)#5 (2) { ["mes":"Month":private]=> string(1) "7" ["year":"Month":private]=> string(4) "2021" } [4]=> object(Month)#6 (2) { ["mes":"Month":private]=> string(1) "8" ["year":"Month":private]=> string(4) "2021" } [5]=> object(Month)#7 (2) { ["mes":"Month":private]=> string(1) "9" ["year":"Month":private]=> string(4) "2021" } [6]=> object(Month)#8 (2) { ["mes":"Month":private]=> string(2) "10" ["year":"Month":private]=> string(4) "2021" } [7]=> object(Month)#9 (2) { ["mes":"Month":private]=> string(2) "11" ["year":"Month":private]=> string(4) "2021" } [8]=> object(Month)#10 (2) { ["mes":"Month":private]=> string(2) "12" ["year":"Month":private]=> string(4) "2021" } [9]=> object(Month)#11 (2) { ["mes":"Month":private]=> string(1) "1" ["year":"Month":private]=> string(4) "2022" } [10]=> object(Month)#12 (2) { ["mes":"Month":private]=> string(1) "2" ["year":"Month":private]=> string(4) "2022" } [11]=> object(Month)#13 (2) { ["mes":"Month":private]=> string(1) "3" ["year":"Month":private]=> string(4) "2022" } [12]=> object(Month)#14 (2) { ["mes":"Month":private]=> string(1) "4" ["year":"Month":private]=> string(4) "2022" } [13]=> object(Month)#15 (2) { ["mes":"Month":private]=> string(1) "5" ["year":"Month":private]=> string(4) "2022" } [14]=> object(Month)#16 (2) { ["mes":"Month":private]=> string(1) "6" ["year":"Month":private]=> string(4) "2022" } }

As you can see the variable in session has changed. It looks like the variable has been serialized, or something like that.

If I put the attributes of the class as public, it works perfectly. But for security I would like them to be private.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

挽梦忆笙歌 2025-02-10 21:29:13

首先,

$messe = $_SESSION['meses']; 
// will create copy of $_SESSION['meses']
// changes to $messe wont be saved to $_SESSION['meses']

您应该使用 alias/coreences/coreences ,即,即:

$messe =& $_SESSION['meses'];
if (!isset($messe)) {
    $messe = getMonths(); 
}
var_dump($_SESSION['meses']);

First of all,

$messe = $_SESSION['meses']; 
// will create copy of $_SESSION['meses']
// changes to $messe wont be saved to $_SESSION['meses']

you should use alias/references, ie:

$messe =& $_SESSION['meses'];
if (!isset($messe)) {
    $messe = getMonths(); 
}
var_dump($_SESSION['meses']);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文