We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 26503
    • 620 Posts
    Modx stores it's session data in the database, serialized [in some format] in the modx_session.data table.column. It looks like this:

    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;


    That is a session record for an anonymous user.

    What format is it in and how do I unserialize it?

    the normal php unserialize($modx_session['data']) returns false.
      *** Not just websites, we also create signage, banners, print, trade show displays and more! ***

      Sean Kimball CLP, CLS.
      Technical Director / Sr. Developer | BigBlock Studios
      ._______________________________________________.
      Bigblock Studios http://www.bigblockstudios.ca Web site design & development.
      27-1300 King Street East. Box 167 Oshawa, Ontario L1H8J4 Canada.
      phone/fax: 905-426-5525
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      That's a JSON string. You should be able to use the MODX API function
      $str = '{name:"John"}'; // or whatever the JSON string is
      $ar = $modx->fromJSON($str);
      print_r($ar); // prints: Array ( 'name' => 'John' )


        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 26503
        • 620 Posts
        first thing I tried, it *looks* like json, but it doesn't validate http://jsonlint.com/ the first bit is not quoted, the pipe means??? and "a:1:{s:3:"web";a:8:" is definitely serialized.

        that's exactly as it appears in the session table.


        Quote from: sottwell at May 28, 2015, 12:01 PM
        That's a JSON string. You should be able to use the MODX API function
        $str = '{name:"John"}'; // or whatever the JSON string is
        $ar = $modx->fromJSON($str);
        print_r($ar); // prints: Array ( 'name' => 'John' )


          *** Not just websites, we also create signage, banners, print, trade show displays and more! ***

          Sean Kimball CLP, CLS.
          Technical Director / Sr. Developer | BigBlock Studios
          ._______________________________________________.
          Bigblock Studios http://www.bigblockstudios.ca Web site design & development.
          27-1300 King Street East. Box 167 Oshawa, Ontario L1H8J4 Canada.
          phone/fax: 905-426-5525
          • 3749
          • 24,544 Posts
          I think the part before the | may be parsed separately and the rest is a JSON string, but I can't find the code that does it.
            Did I help you? Buy me a beer
            Get my Book: MODX:The Official Guide
            MODX info for everyone: http://bobsguides.com/modx.html
            My MODX Extras
            Bob's Guides is now hosted at A2 MODX Hosting
            • 28042 ☆ A M B ☆
            • 24,524 Posts
            I couldn't find it either after an hour or so of code-diving. The whole user/security system is amazingly convoluted.
              Studying MODX in the desert - http://sottwell.com
              Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
              Join the Slack Community - http://modx.org
              • 26503
              • 620 Posts
              Yea - it looks like a sting of serialized array keys, but how they built/split the string is baffling sad
                *** Not just websites, we also create signage, banners, print, trade show displays and more! ***

                Sean Kimball CLP, CLS.
                Technical Director / Sr. Developer | BigBlock Studios
                ._______________________________________________.
                Bigblock Studios http://www.bigblockstudios.ca Web site design & development.
                27-1300 King Street East. Box 167 Oshawa, Ontario L1H8J4 Canada.
                phone/fax: 905-426-5525
                • 18373 ☆ A M B ☆
                • 3,141 Posts
                Why do you need to interact with the session table directly? It's all available in $_SESSION and changes to that are automatically stored at the end of the request.
                  Mark Hamstra • Developer spending his days working on Premium Extras and a MODX Site Dashboard with the ability to remotely upgrade MODX and extras to make the MODX world a little better.

                  Tweet me @mark_hamstra, check my infrequent blog at markhamstra.com, my slightly more frequent ramblings at MODX.today or see code at Github.
                  • 26503
                  • 620 Posts
                  It came up while trying to get hybridauth to work - it does something strange with sessions, while off on a tangent I thought maybe just grab the data and recreate the modx session after being returned from the hybridauth session .... dead end, but it raised this question ..

                  Quote from: markh at May 29, 2015, 08:59 AM
                  Why do you need to interact with the session table directly? It's all available in $_SESSION and changes to that are automatically stored at the end of the request.
                    *** Not just websites, we also create signage, banners, print, trade show displays and more! ***

                    Sean Kimball CLP, CLS.
                    Technical Director / Sr. Developer | BigBlock Studios
                    ._______________________________________________.
                    Bigblock Studios http://www.bigblockstudios.ca Web site design & development.
                    27-1300 King Street East. Box 167 Oshawa, Ontario L1H8J4 Canada.
                    phone/fax: 905-426-5525
                    • 18373 ☆ A M B ☆
                    • 3,141 Posts
                    As I posted on your other thread, there's an existing extra for the HybridAuth library that you may have more luck with wink

                    That data definitely looks serialised to me, but it is not handled in MODX.. rather, it is handled directly in PHP. In modX->_initSession (around line 2400) it loads the modSessionHandler class, and passes that to session_set_save_handler: http://php.net/manual/en/function.session-set-save-handler.php

                    So the exact format of that data may actually depend on the server configuration
                      Mark Hamstra • Developer spending his days working on Premium Extras and a MODX Site Dashboard with the ability to remotely upgrade MODX and extras to make the MODX world a little better.

                      Tweet me @mark_hamstra, check my infrequent blog at markhamstra.com, my slightly more frequent ramblings at MODX.today or see code at Github.
                      • 26503
                      • 620 Posts
                      Quote from: markh at May 29, 2015, 09:26 AM
                      As I posted on your other thread, there's an existing extra for the HybridAuth library that you may have more luck with wink

                      That data definitely looks serialised to me, but it is not handled in MODX.. rather, it is handled directly in PHP. In modX->_initSession (around line 2400) it loads the modSessionHandler class, and passes that to session_set_save_handler: http://php.net/manual/en/function.session-set-save-handler.php

                      So the exact format of that data may actually depend on the server configuration

                      Huh, bingo...

                      so all we need to do to read it [not that I need to read it anymore] is: http://php.net/manual/en/function.session-decode.php

                      sweet - thanks Mark.
                        *** Not just websites, we also create signage, banners, print, trade show displays and more! ***

                        Sean Kimball CLP, CLS.
                        Technical Director / Sr. Developer | BigBlock Studios
                        ._______________________________________________.
                        Bigblock Studios http://www.bigblockstudios.ca Web site design & development.
                        27-1300 King Street East. Box 167 Oshawa, Ontario L1H8J4 Canada.
                        phone/fax: 905-426-5525