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

    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 />

  • Carl

    #2
    Re: Storing objects in a session PHP 5.2.0


    Anthony Smith wrote:
    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 />
    Anthony,

    This is a parse error and most likely not related to objects or
    sessions. My first guess would be a missing semicolon. Check the end of
    line 13 in your vcp_menu2.php source.

    Hope that helps,
    Carl.

    Comment

    • Anthony Smith

      #3
      Re: Storing objects in a session PHP 5.2.0

      The problem was with sing simple_xml I saw a few posts like this but I
      thought it did not apply to what I was doing. Well it did. I just
      needed to cast the xml attribute to a string:

      $role->setSysName((st ring)$xml->roles[$i]->systemName);

      Code Snippet:

      //Turn the xml into an object. Easier to parse this way. Maybe a XSD is
      an option?
      $xml = simplexml_load_ string($xml,'Si mpleXMLElement' ,LIBXML_NOCDATA );

      //First look at the message to see if any roles exist for the user. We
      can look at the
      //roleStatus message which will have a vale or true or false.
      if ($xml->message->roleStatus == "true"){

      //Loop through all of the user's roles
      for($i = 0; $i != count($xml->roles); $i++){

      //Create a role object for each role.
      $role = new Role();
      //Typecast any data from simplexml functions. Otherwise
      //errors will appear such as: "Node no longer exists in
      $role->setSysName((st ring)$xml->roles[$i]->systemName);


      Carl wrote:
      Anthony Smith wrote:
      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 />
      >
      Anthony,
      >
      This is a parse error and most likely not related to objects or
      sessions. My first guess would be a missing semicolon. Check the end of
      line 13 in your vcp_menu2.php source.
      >
      Hope that helps,
      Carl.

      Comment

      Working...