SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict
    Join Date
    Dec 2007
    Posts
    348
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    unserialise session data from mysql db

    Hi guys,

    I'm using a mysql backend for my session handling, and it works absolutely fine. $_SESSION is written to and read correctly, no problems there

    So I want to output a little 'who's online' list on a website. I do a database query "SELECT data FROM sessions..." in the normal way.

    But when I try to unserialise the string, it doesn't work!! (unserialize() returns false) This is a sample string that doesn't unserialise:

    Code:
    user|O:4:"User":3:{s:2:"id";i:0;s:8:"username";s:5:"guest";s:4:"role";s:5:"guest";}
    But if I try to read this value from $_SESSION['user'] then it works fine!! Is there some other processing that needs to be done to successfully unserialise this value?

    it's doing my head in!

  2. #2
    Use The Cloud
    Join Date
    Jan 2006
    Location
    Boise, ID
    Posts
    556
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Could you show us an example of $_SESSION['user'], and an object before it is serialized?

    Edit:

    At first glance it just looks like you have an extra string at the start.

    If you take off the "user|" then the object should unserialize fine.
    Brad Hanson, Web Applications & Scalability Specialist
    ► Is your website outgrowing its current hosting solution?
    ► PM me for a FREE scalability consult!
    ► USA Based: Available by Phone, Skype, AIM, and E-mail.

  3. #3
    SitePoint Evangelist
    Join Date
    Jun 2006
    Location
    Wigan, Lancashire. UK
    Posts
    523
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by old_iron View Post
    But if I try to read this value from $_SESSION['user'] then it works fine!! Is there some other processing that needs to be done to successfully unserialise this value?
    You're not restoring a value, but an object, so you'll need to use the object methods to access the data
    ---
    Development Projects:
    PHPExcel
    PHPPowerPoint

  4. #4
    SitePoint Wizard
    Join Date
    Mar 2008
    Posts
    1,149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's like serialize()'s output, but it's not quite the same. Sessions use a slightly different mechanism to serialize all of its data.

    See http://php.net/session_decode

    However, that function also updates the session with the data you give it. You probably don't want to do that. The comments on that page provide alternative solutions that merely decode the string.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •