Jump to content

Recommended Posts

Hi Freaks, I'm looking for advice if someones willing to give it. Here's the situation ->

I've been working on a project, I started to learn PHP specifically to complete this idea I had. My code has evolved a lot over time as I've started understanding more.

Up until today I've been working on it with just the registration functionality, no login. I had my username hardcoded into the $user_obj instantiation. I decided I wanted to try to make category subscription functionality and doing that I realized I was better off finishing the login form first so as to get a users subscriptions into a session variable at login.

This has brought about the issue of getting an unassigned variable warning from the User class when not logged in. How I made all my other classes was putting a $user in the __construct parameter for each class. I now feel this may have been a rookie error since I'm having problems with error messages especially undefined array keys and variables when there isn't a session started. It's become a bit of a mess.

So the advice I'm looking for and hoping to find here is how you folks handle non $_SESSION sessions, when a user is just scrolling the site not logged in. Did I make a mistake requiring $user for each class __construct? should I move the $user parameter to only the methods that require them? Is there a simpler solution that my inexperience causes to elude me?

What would you folks do in this situation?

Link to comment
https://forums.phpfreaks.com/topic/313618-handling-a-non-logged-in-session/
Share on other sites

You may have made a mistake by requiring that $user. In fact I think you did because that's kinda weird to do. But it's quite possible you could reuse $user and just have it be null for anonymous sessions.

Also, asking questions about specific code tends to go better when you post specific code.

2 hours ago, requinix said:

Also, asking questions about specific code tends to go better when you post specific code.

yea, I understand that, but I felt it wasn't about specific code and more about general advice. If it was in the wrong forum, I apologize.I feel I should only have required $user in specific methods as well. These are growing pains

How you pass the user depends a lot on the architecture of the site code. Current user information is often a singleton/registry, which means not passing $users anywhere, but if you need to perform operations on any user (logged in or not) then yes, you'll be passing user objects around.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.