Since HTTP driven applications are stateless, sessions provide a way to store information about the user across multiple requests. Laravel ships with a variety of session backends that are accessed through an expressive, unified API. Support for popular backends such as Memcached, Redis, and databases is included out of the box.
The session configuration file is stored at config/session.php
. Be sure to review the options available to you in this file. By default, Laravel is configured to use the file
session driver, which will work well for many applications.
The session driver
configuration option defines where session data will be stored for each request. Laravel ships with several great drivers out of the box:
{tip} The array driver is used during testing and prevents the data stored in the session from being persisted.
When using the database
session driver, you will need to create a table to contain the session items. Below is an example Schema
declaration for the table:
Schema::create('sessions', function ($table) {
$table->string('id')->unique();
$table->unsignedInteger('user_id')->nullable();
$table->string('ip_address', 45)->nullable();
$table->text('user_agent')->nullable();
$table->text('payload');
$table->integer('last_activity');
});
You may use the session:table
Artisan command to generate this migration:
php artisan session:table
php artisan migrate
Before using Redis sessions with Laravel, you will need to either install the PhpRedis PHP extension via PECL or install the predis/predis
package (~1.0) via Composer. For more information on configuring Redis, consult its Laravel documentation page.
{tip} In the
session
configuration file, theconnection
option may be used to specify which Redis connection is used by the session.
There are two primary ways of working with session data in Laravel: the global session
helper and via a Request
instance. First, let's look at accessing the session via a Request
instance, which can be type-hinted on a controller method. Remember, controller method dependencies are automatically injected via the Laravel service container:
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class UserController extends Controller
{
/**
* Show the profile for the given user.
*
* @param Request $request
* @param int $id
* @return Response
*/
public function show(Request $request, $id)
{
$value = $request->session()->get('key');
//
}
}
When you retrieve an item from the session, you may also pass a default value as the second argument to the get
method. This default value will be returned if the specified key does not exist in the session. If you pass a Closure
as the default value to the get
method and the requested key does not exist, the Closure
will be executed and its result returned:
$value = $request->session()->get('key', 'default');
$value = $request->session()->get('key', function () {
return 'default';
});
You may also use the global session
PHP function to retrieve and store data in the session. When the session
helper is called with a single, string argument, it will return the value of that session key. When the helper is called with an array of key / value pairs, those values will be stored in the session:
Route::get('home', function () {
// Retrieve a piece of data from the session...
$value = session('key');
// Specifying a default value...
$value = session('key', 'default');
// Store a piece of data in the session...
session(['key' => 'value']);
});
{tip} There is little practical difference between using the session via an HTTP request instance versus using the global
session
helper. Both methods are testable via theassertSessionHas
method which is available in all of your test cases.
If you would like to retrieve all the data in the session, you may use the all
method:
$data = $request->session()->all();
To determine if an item is present in the session, you may use the has
method. The has
method returns true
if the item is present and is not null
:
if ($request->session()->has('users')) {
//
}
To determine if an item is present in the session, even if its value is null
, you may use the exists
method. The exists
method returns true
if the item is present:
if ($request->session()->exists('users')) {
//
}
To store data in the session, you will typically use the put
method or the session
helper:
// Via a request instance...
$request->session()->put('key', 'value');
// Via the global helper...
session(['key' => 'value']);
The push
method may be used to push a new value onto a session value that is an array. For example, if the user.teams
key contains an array of team names, you may push a new value onto the array like so:
$request->session()->push('user.teams', 'developers');
The pull
method will retrieve and delete an item from the session in a single statement:
$value = $request->session()->pull('key', 'default');
Sometimes you may wish to store items in the session only for the next request. You may do so using the flash
method. Data stored in the session using this method will be available immediately and during the subsequent HTTP request. After the subsequent HTTP request, the flashed data will be deleted. Flash data is primarily useful for short-lived status messages:
$request->session()->flash('status', 'Task was successful!');
If you need to keep your flash data around for several requests, you may use the reflash
method, which will keep all of the flash data for an additional request. If you only need to keep specific flash data, you may use the keep
method:
$request->session()->reflash();
$request->session()->keep(['username', 'email']);
The forget
method will remove a piece of data from the session. If you would like to remove all data from the session, you may use the flush
method:
// Forget a single key...
$request->session()->forget('key');
// Forget multiple keys...
$request->session()->forget(['key1', 'key2']);
$request->session()->flush();
Regenerating the session ID is often done in order to prevent malicious users from exploiting a session fixation attack on your application.
Laravel automatically regenerates the session ID during authentication if you are using the built-in LoginController
; however, if you need to manually regenerate the session ID, you may use the regenerate
method.
$request->session()->regenerate();
{note} To utilize session blocking, your application must be using a cache driver that supports atomic locks. Currently, those cache drivers include the
memcached
,dynamodb
,redis
, anddatabase
drivers. In addition, you may not use thecookie
session driver.
By default, Laravel allows requests using the same session to execute concurrently. So, for example, if you use a JavaScript HTTP library to make two HTTP requests to your application, they will both execute at the same time. For many applications, this is not a problem; however, session data loss can occur in a small subset of applications that make concurrent requests to two different application endpoints which both write data to the session.
To mitigate this, Laravel provides functionality that allows you to limit concurrent requests for a given session. To get started, you may simply chain the block
method onto your route definition. In this example, an incoming request to the /profile
endpoint would acquire a session lock. While this lock is being held, any incoming requests to the /profile
or /order
endpoints which share the same session ID will wait for the first request to finish executing before continuing their execution:
Route::post('/profile', function () {
//
})->block($lockSeconds = 10, $waitSeconds = 10)
Route::post('/order', function () {
//
})->block($lockSeconds = 10, $waitSeconds = 10)
The block
method accepts two optional arguments. The first argument accepted by the block
method is the maximum number of seconds the session lock should be held for before it is released. Of course, if the request finishes executing before this time the lock will be released earlier.
The second argument accepted by the block
method is the number of seconds a request should wait while attempting to obtain a session lock. A Illuminate\Contracts\Cache\LockTimoutException
will be thrown if the request is unable to obtain a session lock within the given number of seconds.
If neither of these arguments are passed, the lock will be obtained for a maximum of 10 seconds and requests will wait a maximum of 10 seconds while attempting to obtain a lock:
Route::post('/profile', function () {
//
})->block()
Your custom session driver should implement the SessionHandlerInterface
. This interface contains just a few simple methods we need to implement. A stubbed MongoDB implementation looks something like this:
<?php
namespace App\Extensions;
class MongoSessionHandler implements \SessionHandlerInterface
{
public function open($savePath, $sessionName) {}
public function close() {}
public function read($sessionId) {}
public function write($sessionId, $data) {}
public function destroy($sessionId) {}
public function gc($lifetime) {}
}
{tip} Laravel does not ship with a directory to contain your extensions. You are free to place them anywhere you like. In this example, we have created an
Extensions
directory to house theMongoSessionHandler
.
Since the purpose of these methods is not readily understandable, let's quickly cover what each of the methods do:
Once your driver has been implemented, you are ready to register it with the framework. To add additional drivers to Laravel's session backend, you may use the extend
method on the Session
facade. You should call the extend
method from the boot
method of a service provider. You may do this from the existing AppServiceProvider
or create an entirely new provider:
<?php
namespace App\Providers;
use App\Extensions\MongoSessionHandler;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\ServiceProvider;
class SessionServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
//
}
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
Session::extend('mongo', function ($app) {
// Return implementation of SessionHandlerInterface...
return new MongoSessionHandler;
});
}
}
Once the session driver has been registered, you may use the mongo
driver in your config/session.php
configuration file.