-forgot- Storing objects in a session PHP 5.2.0

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Anthony Smith

    #1

    -forgot- Storing objects in a session PHP 5.2.0

    I have read the message boards in this group and others. I still have
    not been able to pull my obect out of a session and use it. Here is how
    I store it in a session:

    <?
    //Include the UserClass & RoleClass so that we can create a user
    object.
    require_once("U serClass.php");
    require_once("R oleClass.php");

    //This has to be the first line of code a file if sessions are used.
    session_start() ;

    //Create a new user object
    $user = new User();
    $user->setName($_SERV ER['HTTP_OBLIX_GIV ENNAME']);

    $role = new Role();
    $role->setSysName($xm l->roles[$i]->systemName);
    //Now add the role to the user object.
    $user->addRole($role) ;

    //Save the user object in the session
    $_SESSION['user'] = $user;
    ?>

    This is how I try to pull the object out:

    <?
    require_once("U serClass.php");
    require_once("R oleClass.php");

    session_start() ;

    //Include the UserClass & RoleClass so that we can create a user
    object.


    //See if there is a user object in the session
    if ( isset($_SESSION['user']) ){

    //Get user object from session
    $user = (User)$_SESSION['user'];
    }

    I have also just tried:
    $user = $_SESSION['user'];

    ?>

    I receive errors like these:
    <b>Parse error</b>: syntax error, unexpected T_VARIABLE in
    <b>/home/as411161/apache/htdocs/common/vcp_menu2.php</bon line
    <b>14</b><br />

    I also get this message:
    <b>Warning</b>: session_start() [<a
    href='function. session-start'>function .session-start</a>]: Node no
    longer exists in
    <b>/home/as411161/apache/htdocs/common/vcp_menu2.php</bon line
    <b>5</b><br />

  • Geoffrey

    #2
    Re: -forgot- Storing objects in a session PHP 5.2.0

    Hi Anthony --

    This may turn out to be a dead end, but I have to ask anyway. :) Is
    auto_start enabled for sessions in your php.ini file? If it is, this
    might be putting the kibosh on your attempts to save an object to a
    session variable.

    Geoffrey


    Anthony Smith wrote:
    I have read the message boards in this group and others. I still have
    not been able to pull my obect out of a session and use it. Here is how
    I store it in a session:
    >
    <?
    //Include the UserClass & RoleClass so that we can create a user
    object.
    require_once("U serClass.php");
    require_once("R oleClass.php");
    >
    //This has to be the first line of code a file if sessions are used.
    session_start() ;
    >
    //Create a new user object
    $user = new User();
    $user->setName($_SERV ER['HTTP_OBLIX_GIV ENNAME']);
    >
    $role = new Role();
    $role->setSysName($xm l->roles[$i]->systemName);
    //Now add the role to the user object.
    $user->addRole($role) ;
    >
    //Save the user object in the session
    $_SESSION['user'] = $user;
    ?>
    >
    This is how I try to pull the object out:
    >
    <?
    require_once("U serClass.php");
    require_once("R oleClass.php");
    >
    session_start() ;
    >
    //Include the UserClass & RoleClass so that we can create a user
    object.
    >
    >
    //See if there is a user object in the session
    if ( isset($_SESSION['user']) ){
    >
    //Get user object from session
    $user = (User)$_SESSION['user'];
    }
    >
    I have also just tried:
    $user = $_SESSION['user'];
    >
    ?>
    >
    I receive errors like these:
    <b>Parse error</b>: syntax error, unexpected T_VARIABLE in
    <b>/home/as411161/apache/htdocs/common/vcp_menu2.php</bon line
    <b>14</b><br />
    >
    I also get this message:
    <b>Warning</b>: session_start() [<a
    href='function. session-start'>function .session-start</a>]: Node no
    longer exists in
    <b>/home/as411161/apache/htdocs/common/vcp_menu2.php</bon line
    <b>5</b><br />

    Comment

    • Kimmo Laine

      #3
      Re: -forgot- Storing objects in a session PHP 5.2.0

      "Anthony Smith" <mrsmithq@hotma il.comwrote in message
      news:1164640923 .876032.181280@ j44g2000cwa.goo glegroups.com.. .
      >I have read the message boards in this group and others. I still have
      not been able to pull my obect out of a session and use it. Here is how
      I store it in a session:
      >
      <?
      //Include the UserClass & RoleClass so that we can create a user
      object.
      require_once("U serClass.php");
      require_once("R oleClass.php");
      >
      //This has to be the first line of code a file if sessions are used.
      session_start() ;
      >
      //Create a new user object
      $user = new User();
      $user->setName($_SERV ER['HTTP_OBLIX_GIV ENNAME']);
      >
      $role = new Role();
      $role->setSysName($xm l->roles[$i]->systemName);
      //Now add the role to the user object.
      $user->addRole($role) ;
      >
      //Save the user object in the session
      $_SESSION['user'] = $user;
      Not like this...
      ?>
      >
      This is how I try to pull the object out:
      Wrong again. You can't store objects into a session per se, you need to
      serialize and unserialize them in order to use them in a session. Here's
      something you should read:
      PHP is a popular general-purpose scripting language that powers everything from your blog to the most popular websites in the world.


      When you store the object to the session, first serialize it. Then, when
      retrieving it, you unserialize it.

      page 1:
      $_SESSION['user'] = serialize($user );

      page 2:
      $user = unserialize($_S ESSION['user']);

      And remember to always include the class definition on all pages where you
      use the class.

      --
      "Ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" - lpk
      http://outolempi.net/ahdistus/ - Satunnaisesti päivittyvä nettisarjis
      spam@outolempi. net | rot13(xvzzb@bhg byrzcv.arg)


      Comment

      Working...