SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot
    Join Date
    May 2001
    Posts
    193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Why does unserialize() kill a $_SESSION var value

    page1.php
    PHP Code:
    $_SESSION['customer'] = serialize($customer);
    echo 
    '<pre>'.print_r$_SESSION1).'</pre>'
    <a href="page2.php">page 2</a>

    page2.php
    PHP Code:
    $customer unserialize($_SESSION['customer']);
    echo 
    '<pre>'.print_r$_SESSION1).'</pre>'
    <a href="page3.php">page 3</a>

    page3.php
    PHP Code:
    echo '<pre>'.print_r$_SESSION1).'</pre>'
    <a href="page2.php">page 2</a>

    $_SESSION['customer'] is set until unserialize() is called, is that the correct behaviour?

  2. #2
    011110010110000101111001 jabird's Avatar
    Join Date
    Aug 2004
    Location
    U.S.
    Posts
    593
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've never used unserialize... but perhaps its because you're undoing $_SESSION... makes since to me that it wouldn't work...
    ~Jabird
    Jabird.com
    If I were binary... I'd be all 1's for you.
    BBCode trouble?

  3. #3
    SitePoint Zealot
    Join Date
    May 2001
    Posts
    193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    huh?

  4. #4
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    storing something in $_SESSION uses serialize as the background storage mechanism. I can't think of a reason why you would want to do it yourself. Try just:

    PHP Code:
     $_SESSION['customer'] = $customer
    and
    PHP Code:
     $customer $_SESSION['customer']; 
    Jason Sweat ZCE - jsweat_php@yahoo.com
    Book: PHP Patterns
    Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
    Detestable (adjective): software that isn't testable.

  5. #5
    SitePoint Zealot
    Join Date
    May 2001
    Posts
    193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yep that was my problem. I thought you had to serialize objects before adding them to the session. Thanks


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
  •