Jump to content

User logout/role switch without lost all $session vars possible?


skeltern
 Share

Recommended Posts

I use the ProcessWire $session API for a new project to store some things there. Cart items, currency and more. It works fine and the limited lifetime of $sessions is OK, I defined just 3 days. There is only an issue with user $session->logout() to logout an user. It removes all items in user $session too. Website users are just "guest" or after login with role "customer". I see two options to solve my issue:

  1. I skip $session API and use instead PHP $_SESSION to keep all vars after $session->logout(). This make things more complicated and I would prefer to use PW API only.
  2. Anyone way to avoid the $session->logout() command. So that logout means remove the role "customer" temporary from user session, be only guest. Without $session->logout(). Mhhmmmm.

Maybe I think wrong, ideas are welcome. BTW, I enjoy it to work with PW. ;)

Link to comment
Share on other sites

OK, I found a solution for my topic. I copy just all $session values to a bypass array, clean up this array from user related data "_user" and push my stored values back to the new session. I think only the $session key "_user" contains sensible data? In sum, user is after logout "guest" again, but my $session vars got not lost...

    # COPY SESSION VARS
    $session_bypass = $session->getAll();

    # LOGOUT USER / RESET SESSION
    if ($user->isLoggedin()) {
        $session->logout();
    }

    # REMOVE USER DATA FROM SESSION COPY > DELETE KEY "_user"
    if (array_key_exists ("_user", $session_bypass)) {
        unset ($session_bypass["_user"]);
    }

    # RESTORE SESSION WITHOUT "_user" KEY
    foreach ($session_bypass as $key => $value) {
        $session->set($key, $value);
    }
  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...