Servage Magazine

Information about YOUR hosting company – where we give you a clear picture of what we think and do!

Using session data in Laravel

Tuesday, November 24th, 2015 by Servage

session-dataThe session is a system whereby your server can store and retrieve information for a given user. Technically it works by generating a unique session id for each new session and whatever is stored in the session is then actually stored on the server. The user’s browser receives a cookie with the session id, and thereby the server knows which user belongs to which session data.

Note how the European Union has imposed regulations on how website owners are required to inform users about the usage of cookies. This comes as a reaction to major data aggregation and privacy discussions with impact on almost any website owner. At least anyone who uses sessions, because they rely on cookies.

Configuring session usage in Laravel

The default implementation of sessions in Laravel relies on a standardized mechanism whereby the entire cookie and session data storage is handled for you. Essentially you just need to decide which storage engine you wish to use. For smaller projects or local development it is fine to use a file based storage. In that case each session gets a little file in the storage part of your Laravel installation where session data is stored. Cleanup routines can participate in removing obsolete session data. However, in multi-server environments where you use multiple web hosting accounts for performance or redundancy you have a problem with the file based engine. You cannot accept that a session and its data is only available on a single machine. What if the user’s next request is handled by a different web server? Therefore you may consider the other options, e.g. a database driven engine instead.

Basic session data operations

The following describes the most fundamental session data operations like setting, getting and removing data.

// Store the name in the session
$name = "John";
$request->session->put( "name", $name );

// Retrieve the name in the session
$name = $request->session->get( "name" );

// Check if the session has a name
if ( $request->session->has( "name" ) )
    // Do something ...

// Retrieve and remove data from the session
$name = $request->session()->pull('name');

// Remove data from the session

// Pushing data onto an array in the session
$request->session()->push( 'items', 'New item');

As you can see it is very easy to work with session data, and the entire underlying functionality comes with the Laravel framework. There is no major drawback to session data and it is required for smart functionality such as remembering users (logins) or data. I.e. remembering the progress through an order process or other settings by users who are not necessarily registered or logged in.

Using session data in Laravel, 4.8 out of 5 based on 5 ratings
Categories: Guides & Tutorials

Keywords: ,

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

No comments yet (leave a comment)

You are welcome to initiate a conversation about this blog entry.

Leave a comment

You must be logged in to post a comment.