Passing Objects in Session Values

  • (2 Pages)
  • +
  • 1
  • 2

29 Replies - 104199 Views - Last Post: 25 May 2011 - 01:03 PM Rate Topic: -----

#1 gilsal  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 38
  • Joined: 11-October 10

Passing Objects in Session Values

Posted 23 May 2011 - 07:40 AM

Hey guys,

I was wondering if passing objects through as session values were possible?

I figured this would add just a little bit more security and I wouldn't need to pass 4 different sessions for records I need for the user login.

Thanks in advance,

gilsal
Is This A Good Question/Topic? 0
  • +

Replies To: Passing Objects in Session Values

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,257
  • Joined: 08-June 10

Re: Passing Objects in Session Values

Posted 23 May 2011 - 07:45 AM

unless they contain resources (i.e. the objects can be serialised), you can also save objects in a session.
Was This Post Helpful? 2
  • +
  • -

#3 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3001
  • View blog
  • Posts: 10,393
  • Joined: 08-August 08

Re: Passing Objects in Session Values

Posted 23 May 2011 - 08:21 AM

Session variables can hold objects, arrays, etc. They can even hold arrays of objects!
Was This Post Helpful? 2
  • +
  • -

#4 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 947
  • View blog
  • Posts: 2,355
  • Joined: 15-February 11

Re: Passing Objects in Session Values

Posted 23 May 2011 - 08:25 AM

This link should be of use to you.
Was This Post Helpful? 2
  • +
  • -

#5 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3001
  • View blog
  • Posts: 10,393
  • Joined: 08-August 08

Re: Passing Objects in Session Values

Posted 23 May 2011 - 08:51 AM

Note that codeprada's link is useful if you want to save states across sessions. Fore example, if you want to save a user's state for when they log back in at some point in the future after sessions have expired. You don't need to serialize if you're just going from one session to the next.

This post has been edited by CTphpnwb: 23 May 2011 - 08:51 AM

Was This Post Helpful? 0
  • +
  • -

#6 gilsal  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 38
  • Joined: 11-October 10

Re: Passing Objects in Session Values

Posted 23 May 2011 - 12:28 PM

I tried passing an object as the value of my session variable.

I believe it to work but When I try to access the objects attributes it returns no errors and is blank.

I'm not sure if I have stared at this set of code for too long that I'm having such a block at understanding this most likely relatively simple issue.

The object has 4 attributes.

But on the second page which requires the session I am unable to access those attributes.

This post has been edited by gilsal: 23 May 2011 - 12:29 PM

Was This Post Helpful? 0
  • +
  • -

#7 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,257
  • Joined: 08-June 10

Re: Passing Objects in Session Values

Posted 23 May 2011 - 12:34 PM

given the code provided, I can see nothing either. (if you know what I mean)
Was This Post Helpful? 1
  • +
  • -

#8 gilsal  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 38
  • Joined: 11-October 10

Re: Passing Objects in Session Values

Posted 23 May 2011 - 12:37 PM

This is how the code is initiated when validation is passed on the login page.
$_SESSION['user']=$user;



This is how the code is basically used in the second page.
Omitting the other attributes since they are roughly at the same general scope as the username.
$user = $_SESSION['user'];
$username = $user->username;



Sorry I though I had pasted a clip of the code on previous post.
Was This Post Helpful? 0
  • +
  • -

#9 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,257
  • Joined: 08-June 10

Re: Passing Objects in Session Values

Posted 23 May 2011 - 12:37 PM

what does the class for $user look like?
Was This Post Helpful? 0
  • +
  • -

#10 gilsal  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 38
  • Joined: 11-October 10

Re: Passing Objects in Session Values

Posted 23 May 2011 - 12:42 PM

I am able to access all of the user attributes on the first page but when I pass it into my session to the next page is when I lose access.
class sessionUserInfo {
	public $username;
	public $time;
	public $name;
	public $email;
	
	public function setUsername($x){
		$this->username = $x;
	}
	public function setTime($x){
		$this->time = $x;
	}
	public function setName($x){
		$this->name = $x;
	}
	public function setEmail($x){
		$this->email = $x;
	}
}


This post has been edited by gilsal: 23 May 2011 - 12:43 PM

Was This Post Helpful? 0
  • +
  • -

#11 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,257
  • Joined: 08-June 10

Re: Passing Objects in Session Values

Posted 23 May 2011 - 12:48 PM

it works for me. maybe you didnít fill the object with values?
Was This Post Helpful? 0
  • +
  • -

#12 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3001
  • View blog
  • Posts: 10,393
  • Joined: 08-August 08

Re: Passing Objects in Session Values

Posted 23 May 2011 - 12:51 PM

Or maybe the OP didn't store the object in a session variable! It's hard to see when looking through a straw.
Was This Post Helpful? 0
  • +
  • -

#13 gilsal  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 38
  • Joined: 11-October 10

Re: Passing Objects in Session Values

Posted 23 May 2011 - 12:56 PM

Still only able to access attributes on first page and not on second... I must have a mis-referenced variable somewhere. Thanks for confirming it works. At least I know it is somewhere in there =)

View PostCTphpnwb, on 23 May 2011 - 12:51 PM, said:

Or maybe the OP didn't store the object in a session variable! It's hard to see when looking through a straw.


I do have some logic on second page to assess if session variable is empty but I'm not sure if there was one to assess if it is an object.

Would =="" not work on objects?
Was This Post Helpful? 0
  • +
  • -

#14 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,257
  • Joined: 08-June 10

Re: Passing Objects in Session Values

Posted 23 May 2011 - 12:58 PM

first thing you should try:
var_dump($_SESSION['user']);


Quote

Would =="" not work on objects?

if you have a __toString() method, it would.
Was This Post Helpful? 0
  • +
  • -

#15 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3001
  • View blog
  • Posts: 10,393
  • Joined: 08-August 08

Re: Passing Objects in Session Values

Posted 23 May 2011 - 01:27 PM

Show us your code!!! That means the code that sets up the object and stores it in a session variable as well as the code that retrieves the object from the session variable. This could be as simple as you not starting a session both times, or it could be something else. We can't tell if we can't see your code.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2