How to view a specific property of multiple objects stored in a session variable PHP?

Viewed 23

How can i show/view the property "people" only of multiple objects that stored in a session variable. I'm having a warning that said "Warning: Attempt to read property "people" on string". I tried foreach and used echo $log->people but no luck. Thanks

Screenshot of Error/Warning i received

Screenshot of Error/Warning i received

Here is where i want to view/show only the propert "people":

    <?php

    session_start();

    $current_count_inside = $_SESSION['inside'];
    $current_count_outside = $_SESSION['outside'];
    $current_count = $_SESSION['count'];

    $data = $_SESSION['logs'];
    foreach ($data as $log) {
      echo $log->people . "<br>";
    }

    echo "<br>";
    echo "<br>";
    echo "<br>";
    echo "<br>";

    echo "inside = " . $current_count_inside . "<br>";
    echo "outside = " . $current_count_outside . "<br>";
    echo "count = " . $current_count . "<br>";
    ?>

Here is where i have my class Log and store new objects(Log) in to a session.

<?php

session_start();

class Log
{
    public $people;
    public $enter;
    public $timestamp;
    public $location;

    public function __construct($people, $enter, $timestamp, $location)
    {
        $this->people = $people;
        $this->enter = $enter;
        $this->timestamp = $timestamp;
        $this->location = $location;
    }
}


if (isset($_POST['submit'])) {

    if ($_POST['people'] <= 0) {
        return header("Location: index.php");
    }

    if ($_POST['people'] == "inside") {
        if ($_POST['people'] + $_SESSION['inside'] >= 50) {
            return header("Location: index.php");
        }
    }

    $new_log = new Log($_POST['people'], true, date("Y/m/d"), $_POST['location']);
    $_SESSION['logs'][] = serialize($new_log);

    if ($_POST['location'] == "inside") {
        $_SESSION['inside'] = $_SESSION['inside'] + $_POST['people'];
        $_SESSION['count'] = $_SESSION['count'] + $_POST['people'];
    }

    if ($_POST['location'] == "outside") {
        $_SESSION['outside'] = $_SESSION['outside'] + $_POST['people'];
        $_SESSION['count'] = $_SESSION['count'] + $_POST['people'];
    }

    header('Location: historiek.php');
}


if (isset($_POST['remove'])) {

    if (intval($_SESSION['count']) <= 0 || intval($_SESSION['count']) - $_POST['remove_people'] < 0) {
        $_SESSION['errors'] = "Probleem";
        return header("Location: index.php");
    }

    $new_log = new Log($_POST['remove_people'], false, date("Y/m/d"), $_POST['location']);
    $_SESSION['logs'][] = serialize($new_log);

    if ($_POST['location'] == "inside") {
        $_SESSION['inside'] = $_SESSION['inside'] - $_POST['remove_people'];
        $_SESSION['count'] = $_SESSION['count'] - $_POST['remove_people'];
    }

    if ($_POST['location'] == "outside") {
        $_SESSION['outside'] = $_SESSION['outside'] - $_POST['remove_people'];
        $_SESSION['count'] = $_SESSION['count'] - $_POST['remove_people'];
    }

    header('Location: historiek.php');
}
1 Answers

You're serializing when you assign to the session variable, so you have to unserialize when you read it.

    $data = unserialize($_SESSION['logs']);

Note that it's not really necessary for you to serialize in the first place. The session system serializes automatically. But you have to be consistent between saving and reading.

Related