Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

clarkeylogic's avatar

Accessing the session payload as a JSON object

In the website I am making I store a channel variable as a session value. When I try and retrieve the session payload from the database I cannot use it as a JSON.

static function getChannelFromSession($session)
{
    $session_payload = base64_decode($session->payload);
    $session_data = json_decode($session_payload);
    dd($session_data);

    return $session_data[0]->channel;
}

This just returns null.

The $session_payload looks like this.

a:7:{s:6:"_token";s:40:"IJopn8j3ObpHpyOtttbOClRg30YLttN11XS1HRpt";s:6:"locale";s:2:"en";s:50:"login_web_59ba36addc2b2f9401580f014c7f58ea4e30989d";i:1;s:9:"_previous";a:1:{s:3:"url";s:39:"http://findoverwatch.com:8080/en/groups";}s:5:"flash";a:2:{s:3:"old";a:0:{}s:3:"new";a:0:{}}s:7:"channel";s:20:"8_Ib4simdejCn_sQAAAV";s:9:"_sf2_meta";a:3:{s:1:"u";i:1483370823;s:1:"c";i:1483370735;s:1:"l";s:1:"0";}}

So, the question is. What is that format above me, and how can I access data from it?

0 likes
2 replies
davielee's avatar
davielee
Best Answer
Level 11

@clarkeylogic You just need to unserialize it and you should be good to go.

public static function getChannelFromSession($session)
{
    $payload = unserialize(base64_decode($session->payload)); // At this point you have an array

    return $payload['channel'];

    // If you want an object instead, you could typecast it to a stdObject.
    // $payload = (object) unserialize(base64_decode($session->payload));

    // return $payload->channel;
}
3 likes

Please or to participate in this conversation.