Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Shelby's avatar

Setting an attribute to a model to be saved across session

I need to set an attribute to my user that is not stored on the database and that need to be saved from the start of the session to the log out.

What I mean is that I would set the attribute "test" to my User model, and I could still access this attribute with the same value as when I set it on another page.

Should I just put this attribute into my session or is there a better way to do this? The thing is that I also use the "remember me" option and I can't afford to lose that attribute.

Thank you.

0 likes
4 replies
bobbybouwmann's avatar

You can either set a cookie or a session for that ;) You can create a new session for your use of course, so you don't lose the remember me option ;)

Shelby's avatar

Thanks, I think using a cookie is the best way.

I still have a question about them though, I will probably use Cookie::forever that I will destroy when the user logs out, but what happens if the user deletes himself the cookie? What will return Cookie::get()? I can't find any documentation about this method online...

Thanks!

pmall's avatar

@Shelby what are you trying to achieve here ? Theres probably a better solution.

Please or to participate in this conversation.