Passing Session Object by reference

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
msurabbott
Forum Commoner
Posts: 33
Joined: Thu Jan 01, 2009 10:18 pm
Location: Chicago, IL, USA

Passing Session Object by reference

Post by msurabbott »

~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.


So I have the following code:

Code: Select all

$authenticatedUser = unserialize($_SESSION['authenticatedUser']);
$locationDS = new LocationDataSource();
$locationDS->updateLocation($_POST, $authenticatedUser->location);
Where the updateLocation method takes a second parameter by reference... and in the method:

Code: Select all

public function updateLocation($data, &$locationObj = null) {
// Confirm that the object being updated is of the correct type, if provided
if($locationObj != null && !is_a($locationObj, 'Location')) {
    return false;
}
        
$sql = "UPDATE $this->table " . 
    "SET add1 = '". $data['add1'] ."', " . 
    "add2 = '". $data['add2'] ."', " . 
    "city = '". $data['city'] ."', " . 
    "state_id = ". $data['state_id'] .", " . 
    "zip = '". $data['zip'] ."' " . 
    "WHERE id = " . (($locationObj != null) ? $locationObj->id : $data['id']);
        
$result = $this->db->query($sql);
        
if(mysql_affected_rows($this->db->conn)) {
    if($locationObj != null) {
        $locationObj->populate($data);
    }
            
    return true;    
}
else {
    return false;
    }
}
it repopulates the objects data ('by reference') and I was thinking that it would modify the values, but it doesn't - since its an object in the session I have to log out, and back in for it to show up as changed.. any ideas as to why?

Thanks in advance!


~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Passing Session Object by reference

Post by requinix »

99% likely:

$authenticatedUser is being modified, but you have to save it back into $_SESSION.

Code: Select all

$authenticatedUser = unserialize($_SESSION['authenticatedUser']);
$locationDS = new LocationDataSource();
$locationDS->updateLocation($_POST, $authenticatedUser->location);
$_SESSION['authenticatedUser'] = serialize($authenticatedUser);
Alternatively, assign $authenticatedUser by reference:

By the way, stuff in $_SESSION is automatically serialized. Meaning you don't need to use serialize/unserialize:

Code: Select all

$authenticatedUser = $_SESSION['authenticatedUser'];
$locationDS = new LocationDataSource();
$locationDS->updateLocation($_POST, $authenticatedUser->location);
$_SESSION['authenticatedUser'] = $authenticatedUser;
 
// but since $authenticatedUser is a copy of another variable 
$authenticatedUser =& $_SESSION['authenticatedUser']; // by reference
$locationDS = new LocationDataSource();
$locationDS->updateLocation($_POST, $authenticatedUser->location);
// no saving necessary
 
// but that's still more work than necessary: just use $_SESSION[authenticatedUser] directly
$locationDS = new LocationDataSource();
$locationDS->updateLocation($_POST, $_SESSION['authenticatedUser']->location);
msurabbott
Forum Commoner
Posts: 33
Joined: Thu Jan 01, 2009 10:18 pm
Location: Chicago, IL, USA

Re: Passing Session Object by reference

Post by msurabbott »

tasairis I tried your last solution before I posted, and found that the unserialize was necessary.. if I do what you suggested last I get null back for the location object, where as if I unserialize it I get the object just fine (so its not that the object inst loaded properly).

This was the case with all objects in the session, location is an object, of the user object - so because of this I added the serialize/unserialize and things began to work.

I have also tried the solution second on your list with no luck.. nothing seems to be saving, or updating by reference correctly..
msurabbott
Forum Commoner
Posts: 33
Joined: Thu Jan 01, 2009 10:18 pm
Location: Chicago, IL, USA

Re: Passing Session Object by reference

Post by msurabbott »

I really need help on this.. anyone else out there think they could provide a little assistance?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Passing Session Object by reference

Post by requinix »

Huh, maybe the serialization thing is new to PHP 5.

Just to make sure, you tried the first solution I gave, right?
msurabbott
Forum Commoner
Posts: 33
Joined: Thu Jan 01, 2009 10:18 pm
Location: Chicago, IL, USA

Re: Passing Session Object by reference

Post by msurabbott »

Yes, I tried them all with no success.. I am currently having to reload the user object every time the page loads, and reassign to the session obj.. which is obviously not desirable.
Post Reply