Overview
Sessions provide a way to store information about the user across multiple requests. Laravel supports various session backends through a unified API, configured inconfig/session.php.
Default Session Driver
.env
config/session.php
Available Session Drivers
Laravel supports these session drivers:file- File-based sessions instorage/framework/sessionscookie- Encrypted cookie storagedatabase- Database table storagememcached- Memcached serverredis- Redis serverdynamodb- Amazon DynamoDBarray- In-memory storage (testing only)
File Driver
Stores sessions as files on disk..env
config/session.php
Ensure the
storage/framework/sessions directory is writable by the web server.Cookie Driver
Stores encrypted session data in cookies..env
Database Driver
Stores sessions in a database table. Recommended for most applications.Environment Variables
.env
Configuration
config/session.php
Setup
Create the sessions table:database/migrations/0001_01_01_000000_create_users_table.php
The database driver provides better scalability than file-based sessions for multi-server setups.
Redis Driver
Uses Redis for high-performance session storage..env
config/session.php
Memcached Driver
Uses Memcached for distributed session storage..env
DynamoDB Driver
Uses Amazon DynamoDB for cloud-based session storage..env
Session Lifetime
Control how long sessions remain valid:.env
config/session.php
SESSION_LIFETIME
SESSION_LIFETIME
Number of minutes before the session expires due to inactivity.Default:
120 minutes (2 hours)SESSION_EXPIRE_ON_CLOSE
SESSION_EXPIRE_ON_CLOSE
When
true, sessions expire when the browser closes.Default: falseSession Encryption
Encrypt all session data before storage:.env
config/session.php
The cookie driver always encrypts data. Other drivers only encrypt if this option is enabled.
Session Sweeping
Automatically clean up old sessions:config/session.php
Cookie Configuration
Control session cookie behavior:Cookie Name
.env
config/session.php
Cookie Path and Domain
.env
config/session.php
Security Options
.env
config/session.php
secure
secure
Only send cookies over HTTPS connections.Recommended:
true in productionhttp_only
http_only
Prevent JavaScript access to the cookie.Recommended:
true (prevents XSS attacks)same_site
same_site
Control cross-site request behavior.Options:
lax, strict, none, nullDefault: lax (good balance of security and usability)partitioned
partitioned
Enable partitioned cookies for cross-site contexts.Default:
falseRequires
secure to be true and same_site to be noneUsing Sessions
Storing Data
Retrieving Data
Flash Data
Store data for the next request only:Deleting Data
Session Cache Store
For cache-driven session backends:.env
config/session.php
Applies to
dynamodb, memcached, and redis drivers. Must match a defined cache store.Best Practices
Use database or Redis in production
File and cookie drivers don’t scale well across multiple servers.
Artisan Commands
Next Steps
Queue Configuration
Configure background job processing
Cache Configuration
Set up application caching