Jump to content

Object stored in Session?


Cine

Recommended Posts

Hello,

 

Here's my system. Once a user is successfully logged in, a new instance of a User class is created. The constructor of this class grabs the details of the logged-in user from a database and stores them inside properties in the User class.

 

The problem is, obviously I'd want to access these properties from any page I require them to be able to display user data, so I looked into storing the User object in a Session.

 

When I tried implementing this, I ran into a bunch of errors and I couldn't figure it out.

 

Here's an example:

 

After a user has logged in, I had the following code:

$_SESSION['user'] = new User($this->username);

 

I was under the impression that this assigns a user object to a session. But it's not working as I receive this error:

 

Notice: Undefined index: user in ../v2/admin/index.php on line 18

 

 

Then on the page I want to display the name of the current user logged-in, I had this code:

$_SESSION['user']->get_Name();

 

But then I get this error:

Fatal error: Call to a member function get_IP() on a non-object in ../v2/admin/index.php on line 18

 

 

Can tell me what I have to do, to make this work?

 

 

Thanks.

 

Link to comment
Share on other sites

Sessions are unable to store certain object structures.  To get around this, try to serialize() your object and then store that as the session, and then unserialize() it when you need to use it.

Link to comment
Share on other sites

hi there,

 

If you want to store your object in the session variable, remember to include all the dependent object files in order to work.

 

example:

 

include_once('myobject.php');

 

$_SESSION['myobject']->myfunction();

 

 

I haven't test the above code, but that's how I understand the object work.

Link to comment
Share on other sites

It sounds like you don't have a session_start(); statement on every page that sets or references a session variable and the class definition needs to exist before the session_start(); statement so that the object can be recreated when the session start causes it to be resumed.

 

@nethnet, the only things that sessions cannot carry between pages are resources, because resources are destroyed when execution ends on any page. Session data is serialized internally when it is saved and unserialized internally when it is restored. Serializing an object to store it in a session just results in double serialization of the data. If you don't want to create the object directly in a session variable (what the OP is doing), you can simply assign ( an = operator ) an existing object to a session variable to save it.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.