DEV Community

Cover image for Fixing Auto Logout in Laravel When Running the Same Project on Different Ports
Golam Rahman Sagor
Golam Rahman Sagor

Posted on

Fixing Auto Logout in Laravel When Running the Same Project on Different Ports

When running the same Laravel application on multiple ports (e.g., 8000 and 9000) in a local environment, you may encounter a frustrating issue: logging into one instance automatically logs you out of the other. This is due to shared session cookies, which conflict between the two ports.
In this guide, we'll cover two approaches to fix this issue: a basic solution for most use cases and an advanced solution for more granular session management.


Part 1: Basic Solution

The simplest way to solve this issue is by assigning a unique session cookie name for each port. Laravel identifies sessions using the SESSION_COOKIE value set in the .env file.
1. Modify the .env File
Open the .env file for each instance and set a unique SESSION_COOKIE value:

For port 8000:

SESSION_COOKIE=laravel_session_8000
Enter fullscreen mode Exit fullscreen mode

For port 9000:

SESSION_COOKIE=laravel_session_9000
Enter fullscreen mode Exit fullscreen mode

2. Clear Configuration Cache

After updating the .env file, clear the configuration cache to apply changes:

php artisan config:clear
Enter fullscreen mode Exit fullscreen mode

That’s it! Now, each port will use its own session cookie, and logging into one instance will no longer log you out of the other.


Part 2: Advanced Solution

For scenarios where more isolation is needed (e.g., different environments or separate session storage), you can use advanced configurations.

1. Use Environment-Specific Files
Create separate .env files for each instance, such as .env.8000 and .env.9000.

  • In .env.8000, set:
SESSION_COOKIE=laravel_session_8000
Enter fullscreen mode Exit fullscreen mode
  • In .env.9000, set:
SESSION_COOKIE=laravel_session_9000
Enter fullscreen mode Exit fullscreen mode

Run each instance with the appropriate .env file:

php artisan serve --env=.env.8000 --port=8000    
php artisan serve --env=.env.9000 --port=9000
Enter fullscreen mode Exit fullscreen mode

2. Separate Session Storage
If you want complete isolation of sessions:
Use the file driver with unique session paths for each instance:
For port 8000:

SESSION_DRIVER=file    
SESSION_PATH=storage/framework/sessions_8000
Enter fullscreen mode Exit fullscreen mode

For port 9000:

SESSION_DRIVER=file    
SESSION_PATH=storage/framework/sessions_9000
Enter fullscreen mode Exit fullscreen mode

Create the directories:

mkdir -p storage/framework/sessions_8000    
mkdir -p storage/framework/sessions_9000    
chmod -R 775 storage/framework/sessions_8000    
chmod -R 775 storage/framework/sessions_9000
Enter fullscreen mode Exit fullscreen mode

3. Clear All Caches
To ensure all configurations are applied correctly:

php artisan cache:clear    
php artisan config:clear    
php artisan route:clear    
php artisan view:clear
Enter fullscreen mode Exit fullscreen mode

If you’re running multiple instances of a Laravel application on different ports, the basic solution of setting unique session cookie names is often sufficient. However, if you need greater isolation, the advanced solution provides more control and flexibility.

Choose the approach that best fits your needs, and enjoy seamless local development with Laravel!

Have any questions or tips you’d like to add?
Feel free to drop them in the comments below!

Let’s level up our Laravel projects together.

Thanks for taking the time to read my article!

— Sagor

Top comments (0)