yougotnet's avatar

Issue with memached user two different browsers

When a user logs into the site, I use memcache to cache their user model.

On the same computer, I used Chrome and logged in as User1, then in Safari logged in as User2. When I got back to Chrome and refresh the page, User2 now shows as logged in and not User1.

0 likes
2 replies
LaryAI's avatar
Level 58

The issue you're experiencing is likely due to the way you're using memcached to store user session data. Memcached is a distributed memory caching system, and it's not designed to handle session data that needs to be unique per user session. When you cache the user model in memcached, it doesn't differentiate between different sessions or browsers, which is why you're seeing the same user across different browsers.

To resolve this issue, you should use a session management system that is designed to handle per-user sessions. Here are a few steps you can take:

  1. Use a Session Store: Instead of using memcached for user sessions, use a session store that is designed for this purpose. Common options include:

    • Database-backed sessions: Store session data in your database.
    • File-based sessions: Store session data in files on the server.
    • Redis: A popular choice for session storage due to its speed and persistence.
  2. Configure Your Application: If you're using a framework like Laravel, you can easily configure the session driver in the configuration file. For example, in Laravel, you can set the session driver in config/session.php:

    'driver' => env('SESSION_DRIVER', 'file'),
    

    You can change 'file' to 'database', 'redis', or another supported driver.

  3. Ensure Session Isolation: Make sure that each user session is isolated and not shared across different browsers or devices. This is typically handled by the session management system.

  4. Test Across Browsers: After making these changes, test logging in with different users across different browsers to ensure that sessions are handled correctly and independently.

By using a proper session management system, you can ensure that each user's session is isolated and that logging in from different browsers will not interfere with each other.

JussiMannisto's avatar

That's how cache works. It's global. You shouldn't use it for storing auth data — that's what the session is for.

But you shouldn't store the entire user model in the session either, as Laravel already handles these things for you. The logged in user model is automatically pulled from the DB on every request, and you can call Auth::user() to get it.

Please or to participate in this conversation.