如何反序列化modx_session表数据?

时间:2015-05-28 02:04:07

标签: php serialization modx-revolution

Modx将它的会话数据存储在数据库中,在modx_session.data table.column中以[某种格式]序列化。它看起来像这样:

  

modx.user.0.resourceGroups |一个:1:{S:3:"网络&#34 ;;一个:8:{I:0; S:2:" 10" ; I:1; S:2:" 12&#34 ;; I:2; S:2:" 13&#34 ;; I:3; S:2:" 14&#34 ;; I:4; S:1:" 3&#34 ;; I:5; S:1:" 7&#34 ;; I:6; S:1:" 8&# 34 ;; I:7; S:1:" 9&#34 ;;}} modx.user.0.attributes |一个:1:{S:3:"网络&#34 ;;一个: 4:{S:16:" modAccessContext&#34 ;;一个:1:{S:3:"网络&#34 ;;一个:1:{I:0;一个:3:{S: 9:"主&#34 ;; I:0; S:9:"权威&#34 ;; S:1:" 0&#34 ;; S:6:"策略& #34 ;;一个:1:{S:4:"负载&#34 ;; b:1;}}}} S:22:" modAccessResourceGroup&#34 ;;一个:8:{I: 10;一个:1:{I:0;一个:3:{S:9:"主&#34 ;; I:0; S:9:"权威&#34 ;; S:1: #&34; 0&#34 ;; S:6:"策略&#34 ;;一个:1:{S:4:"负载&#34 ;; b:1;}}} I:12 ;一个:1:{I:0;一个:3:{S:9:"主&#34 ;; I:0; S:9:"权威&#34 ;; S:1:& #34; 0&#34 ;; S:6:"策略&#34 ;;一个:1:{S:4:"负载&#34 ;; b:1;}}} I:13;一个:1:{I:0;一个:3:{S:9:"主&#34 ;; I:0; S:9:"权威&#34 ;; S:1:&# 34; 0&#34 ;; S:6:"策略&#34 ;;一个:1:{S:4:"负载&#34 ;; b:1;}}} I:14; :1:{I:0;一个:3:{S:9:"主&#34 ;; I:0; S:9:"权威&#34 ;; S:1: #&34; 0&#34 ;; S:6:"策略&#34 ;;一个:1:{S:4:"负载&#34 ;; B:1;}}} I:3 ;一个:1:{I:0;一个:3:{S:9:"主&#34 ;; I:0; S:9:"权威&#34 ;; S:1:& #34; 0&#34 ;; S:6:"策略&#34 ;;一个:1:{S:4:"负载&#34 ;; b:1;}}} I:7;一个:1:{I:0;一个:3:{S:9:"主&#34 ;; I:0; S:9:"权威&#34 ;; S:1:&# 34; 0&#34 ;; S:6:"策略&#34 ;;一个:1:{S:4:"负载&#34 ;; b:1;}}} I:8;一个:1:{I:0;一个:3:{S:9:"主&#34 ;; I:0; S:9:"权威&#34 ;; S:1:#34& ; 0&#34 ;; S:6:"策略&#34 ;;一个:1:{S:4:"负载&#34 ;; b:1;}}} I:9;: 1:{I:0;一个:3:{S:9:"主&#34 ;; I:0; S:9:"权威&#34 ;; S:1:" 0&#34 ;; S:6:"策略&#34 ;;一个:1:{S:4:"负载&#34 ;; b:1;}}}} S:17:&# 34; modAccessCategory&#34 ;;一个:0:{} S:28:" sources.modAccessMediaSource&#34 ;;一个:0:{}}} modx.user.contextTokens |一个:0:{} this_anonymous_pagecount | I:2;

这是匿名用户的会话记录。

它是什么格式以及如何反序列化?

正常的php unserialize($ modx_session [' data'])返回false。

1 个答案:

答案 0 :(得分:1)

由于MODX modSession扩展为xPDOObject,所以最好通过一些xPDO / MODX对象代码来读取它。

  

更新:感谢@jason-coward解析会话数据或在MDOX代码段中使用它的方法是:

modSession->get(‘data’)

但是如果我们去了vanilla php route,我通过php的session_decode函数得到了解码的数据。

快速php代码:

<?php
session_start();
$input = 'modx.user.0.resourceGroups|a:1:{s:3:"web";a:8:{i:0;s:2:"10";i:1;s:2:"12";i:2;s:2:"13";i:3;s:2:"14";i:4;s:1:"3";i:5;s:1:"7";i:6;s:1:"8";i:7;s:1:"9";}}modx.user.0.attributes|a:1:{s:3:"web";a:4:{s:16:"modAccessContext";a:1:{s:3:"web";a:1:{i:0;a:3:{s:9:"principal";i:0;s:9:"authority";s:1:"0";s:6:"policy";a:1:{s:4:"load";b:1;}}}}s:22:"modAccessResourceGroup";a:8:{i:10;a:1:{i:0;a:3:{s:9:"principal";i:0;s:9:"authority";s:1:"0";s:6:"policy";a:1:{s:4:"load";b:1;}}}i:12;a:1:{i:0;a:3:{s:9:"principal";i:0;s:9:"authority";s:1:"0";s:6:"policy";a:1:{s:4:"load";b:1;}}}i:13;a:1:{i:0;a:3:{s:9:"principal";i:0;s:9:"authority";s:1:"0";s:6:"policy";a:1:{s:4:"load";b:1;}}}i:14;a:1:{i:0;a:3:{s:9:"principal";i:0;s:9:"authority";s:1:"0";s:6:"policy";a:1:{s:4:"load";b:1;}}}i:3;a:1:{i:0;a:3:{s:9:"principal";i:0;s:9:"authority";s:1:"0";s:6:"policy";a:1:{s:4:"load";b:1;}}}i:7;a:1:{i:0;a:3:{s:9:"principal";i:0;s:9:"authority";s:1:"0";s:6:"policy";a:1:{s:4:"load";b:1;}}}i:8;a:1:{i:0;a:3:{s:9:"principal";i:0;s:9:"authority";s:1:"0";s:6:"policy";a:1:{s:4:"load";b:1;}}}i:9;a:1:{i:0;a:3:{s:9:"principal";i:0;s:9:"authority";s:1:"0";s:6:"policy";a:1:{s:4:"load";b:1;}}}}s:17:"modAccessCategory";a:0:{}s:28:"sources.modAccessMediaSource";a:0:{}}}modx.user.contextTokens|a:0:{}this_anonymous_pagecount|i:2;';
$output = session_decode($input);
$ses = $_SESSION;
echo "<pre>";
print_r($ses);
?>

以下是解码数据:

Array
(
    [modx.user.0.resourceGroups] => Array
    (
        [web] => Array
        (
            [0] => 10
            [1] => 12
            [2] => 13
            [3] => 14
            [4] => 3
            [5] => 7
            [6] => 8
            [7] => 9
            )

        )

    [modx.user.0.attributes] => Array
    (
        [web] => Array
        (
            [modAccessContext] => Array
            (
                [web] => Array
                (
                    [0] => Array
                    (
                        [principal] => 0
                        [authority] => 0
                        [policy] => Array
                        (
                            [load] => 1
                            )

                        )

                    )

                )

            [modAccessResourceGroup] => Array
            (
                [10] => Array
                (
                    [0] => Array
                    (
                        [principal] => 0
                        [authority] => 0
                        [policy] => Array
                        (
                            [load] => 1
                            )

                        )

                    )

                [12] => Array
                (
                    [0] => Array
                    (
                        [principal] => 0
                        [authority] => 0
                        [policy] => Array
                        (
                            [load] => 1
                            )

                        )

                    )

                [13] => Array
                (
                    [0] => Array
                    (
                        [principal] => 0
                        [authority] => 0
                        [policy] => Array
                        (
                            [load] => 1
                            )

                        )

                    )

                [14] => Array
                (
                    [0] => Array
                    (
                        [principal] => 0
                        [authority] => 0
                        [policy] => Array
                        (
                            [load] => 1
                            )

                        )

                    )

                [3] => Array
                (
                    [0] => Array
                    (
                        [principal] => 0
                        [authority] => 0
                        [policy] => Array
                        (
                            [load] => 1
                            )

                        )

                    )

                [7] => Array
                (
                    [0] => Array
                    (
                        [principal] => 0
                        [authority] => 0
                        [policy] => Array
                        (
                            [load] => 1
                            )

                        )

                    )

                [8] => Array
                (
                    [0] => Array
                    (
                        [principal] => 0
                        [authority] => 0
                        [policy] => Array
                        (
                            [load] => 1
                            )

                        )

                    )

                [9] => Array
                (
                    [0] => Array
                    (
                        [principal] => 0
                        [authority] => 0
                        [policy] => Array
                        (
                            [load] => 1
                            )

                        )

                    )

                )

[modAccessCategory] => Array
(
    )

[sources.modAccessMediaSource] => Array
(
    )

)

)

[modx.user.contextTokens] => Array
(
    )

[this_anonymous_pagecount] => 2
)

相关信息:

modSession类https://github.com/modxcms/revolution/blob/master/core/model/modx/modsessionhandler.class.php#L170

modSession API文档http://api.modx.com/revolution/2.2/db_core_model_modx_modsession.class.html#%5CmodSession