Cine Posted April 14, 2011 Share Posted April 14, 2011 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 https://forums.phpfreaks.com/topic/233687-object-stored-in-session/ Share on other sites More sharing options...
nethnet Posted April 14, 2011 Share Posted April 14, 2011 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 https://forums.phpfreaks.com/topic/233687-object-stored-in-session/#findComment-1201462 Share on other sites More sharing options...
php-lover Posted April 14, 2011 Share Posted April 14, 2011 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 https://forums.phpfreaks.com/topic/233687-object-stored-in-session/#findComment-1201485 Share on other sites More sharing options...
PFMaBiSmAd Posted April 14, 2011 Share Posted April 14, 2011 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 https://forums.phpfreaks.com/topic/233687-object-stored-in-session/#findComment-1201582 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.