• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 188
  • Last Modified:

Getting an object from a session variable.

I have the following:

<?php
      include "includes/classes/Owner.php";
 
  session_start();
 
  $newOwner = new Owner();
 
  if(isset($_SESSION['NewOwner'])) {
     $newOwner = $_SESSION['NewOwner'];
  }

blah blah

Is this the right way to get an object from a session variable?

I have just read something saying that there is no need to serialise/unserialise session vars, is this correct?

also once the data on this owner is collected it is posted to a php page that populates an owner object and I then want to set the object to a session var of the same name as this one i.e NewOwner.

At the mo on this page I have:
$_SESSION['NewOwner']=$owner;

where $owner is my populated class.

Could someone give me some pointers as to how to set the NewOwner session var if this is incorrect.

Thanks in advance.

Simon
0
scm0sml
Asked:
scm0sml
  • 5
1 Solution
 
scm0smlAuthor Commented:
when i say session variable i actually mean session.

Basically I will have to sessions running.....NewCaravan and NewOwner.
0
 
dr_dedoCommented:
you have to serialize your object before assigning it to a session variable
<?
class mine {
      public $c;
}
session_start();
$t = new mine();
$t->c=6;
$_SESSION['obj']= serialize($t);
// to use that variable
$r = unserialize($_SESSION['obj']);
echo $r->c;
?>
0
 
scm0smlAuthor Commented:
I'm now serializing when writing to the session and unserializing when getting from.

Is it enough to populate an object...then assign that object to the session and then when i get it back just assign my session variable to the class?

I'm doing this at the mo.....

but if i enter I value and then assign the object to the session....then use the back button on the browser, the value does not show up??

shouldnt it be showing up?

I'm assigning the vlaue of the text box to the value of the class member....that should have been populated when I got the session variable?

Any ideas?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
scm0smlAuthor Commented:
this is the code i have to write to the session variable....just using the title as an example.

<?php                              
      session_start();
      
      include "includes/classes/Owner.php";
      
      import_request_variables('p', 'p_');
            
      //sends an owner class data to owner session
      function SendOwnerToSessionVar()
      {            
            global $p_title;
                                    
            $owner = new Owner();
            
            $owner->Title = $p_title;
                        
            $_SESSION['NewOwner']=serialize($owner);      
            
            echo "Complete";
      }      
      SendOwnerToSessionVar();            
?>
0
 
scm0smlAuthor Commented:
and on the form that gets the data and displays it i have:

<?php
      include "includes/classes/Owner.php";
 
  session_start();
 
  $newOwner = new Owner();
 
  if(isset($_SESSION['NewOwner'])) {
     $newOwner = $_SESSION['NewOwner'];
  }

//then the html to display it......just using Forename as an example
<input type="text" name="forename" value="<?=$newOwner->Forename?>" id="forename" size="25" maxlength="50" title="Your First name" />
0
 
scm0smlAuthor Commented:
whoooops!!

just solved my own prob.......i wasnt unserialising the session when getting it to display.

cheers!

easy points for u!
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now