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. But couldn't figure out the best way to do it.

I then fount out you could actually assign an object to a session. Which would be perfect for what I need.

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 have 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.

    Did you make sure that the 'User' class was defined before calling [man]session_start/man? Objects stored in sessions are serialized so that they can be written to disk as plain text. If you call session_start() before the User class is defined (or if you simply never define it on every page that uses sessions) then the PHP session handler won't be able to unserialize the text back into an object and you'll have lost it for good.

      bradgrafelman;10978052 wrote:

      Did you make sure that the 'User' class was defined before calling [man]session_start/man? Objects stored in sessions are serialized so that they can be written to disk as plain text. If you call session_start() before the User class is defined (or if you simply never define it on every page that uses sessions) then the PHP session handler won't be able to unserialize the text back into an object and you'll have lost it for good.

      So I have to include the User class on every page that I'm interacting with sessions?

        Cine wrote:

        So I have to include the User class on every page that I'm interacting with sessions?

        I do believe so, yes; otherwise, on any one page where you don't include it, and the PHP session handler fails to build the object in $_SESSION['user'], at the end of the request the session will be written back to disk without the User object.

          bradgrafelman;10978054 wrote:

          I do believe so, yes; otherwise, on any one page where you don't include it, and the PHP session handler fails to build the object in $_SESSION['user'], at the end of the request the session will be written back to disk without the User object.

          Ah ok.

          I've just included that on the page where I'm displaying the user's name, but still receive the errors.

          sesstion_start(); is used in the constructor method of the Login class. I'm assigning the User object to $_SESSION['user'] later on in the class when the user has logged in.

            Do you have error_reporting set to E_ALL and display_errors (or log_errors) set to On? If so, are you getting any PHP error messages?

            Also, try outputting the value of [man]session_id/man on the different pages - make sure that the ID never changes; if it does, then your session ID (SID) isn't being propogated from one request to the next (e.g. via cookies).

              Sorry, I'm dumb.

              I logged in before I made these changes, and hadn't cleared the session data after I implemented this.

              Just cleared by browser data and it's working fine now.

              Thanks for all the help!

                Glad I could help; don't forget to mark this thread resolved (if it is) using the link on the Thread Tools menu above.

                  Write a Reply...