Laravel session


Laravel session – : Let us have a look over session and how it works?
Breif about Session –
Sessions are used to store information on server side which are available throughout the application. Sessions are stored on server side but they are dependent on cookie as session ids are maintained on browser.

Note : Sessions will not work if you delete cookie of the browser as the id of the sessions will be deleted.

Laravel session provides the functionality to store information accross the application. You can store Laravel sessions in files or database.

Tip :Store sessions in database in when you are working with secure data.
Note : Storing sessions in database makes a little bit slower than storing in file system.

Laravel Session – Set/Get Syntax And Example

Let us understand laravel session syntax with example-

Configurations

Session configuration file is config/session.php. Go and add the required parameters.

Here is config/session.php file with default settings-

session.php

session.php


return [

    'driver' => env('SESSION_DRIVER', 'file'),
    'lifetime' => 120,
    'expire_on_close' => false,
    'encrypt' => false,
    'files' => storage_path('framework/sessions'),
    'connection' => null,
    'table' => 'sessions',
    'lottery' => [2, 100],
    'cookie' => 'laravel_session',
    'path' => '/',
    'domain' => null,
    'secure' => false,
];

  • files – Files to store sessions located at – storage/framework/sessions
  • Cookies – Stored at client side in encrypted form.
  • Database table – Table to store sessions by default it is “sessions”
  • Secure – By default it is false if you are working with https turn it as true.
  • Memcached/Redis – Used for faster performance as it provides faster storage.
  • Array – Are stored in variables only and not available throughout the application.

Create Session In Laravel –

Here is syntax to create session variable using the session function. put keyword is used to set the session variable.

Syntax

Session::put(Key, value);

Where key is name of key you want to store the value.

Example

$userid = "98234324";
Session::put('userid', $userid);

Retrive Value From laravel Session

Here is syntax to retrive session value –

Syntax

Session::get(Key, value);

Example


$userid = Session::get('userid');
echo $userid;

Which will print98234324

Delete Data from laravel session

forget keyword is used to delete data from sessions.

Here is syntax to delete data from session in laravel –

Syntax

Session::forget(key);

Example


Session::forget('userid');

Which will delete userid from session.

Delete All Data From Session

If you want to clear all variables of session use flush() keyword.

Syntax

Session::flush();

Which will clear all session variables throughout the system.

Store Array Values in Session-

If you are working with array you can use following method to store values as –

Here is syntax to store array-

Syntax

Session::push(key,array);

Where first parameter is key and second parameter is array.

Example

$data = array("name"=>"John","email"=>"john@example.com");
Session::push('userinfo',$data);

Getting array data from session-

Example

$user = Session::get('userinfo');
echo $user[0]['name']."<br>";
// print John
echo $user[0]['email'];
// print john@example.com

Updating Array Index In Session-

Use following method to update array index in session –

Example

Session::push('userinfo.name','Lee');

Which will update the array index name’s value as “Lee”

laravel session example get set session variable in laravel

If want to read full documentation about laravel sessions you can refer laravel.com docs – http://laravel.com/docs/5.1/session


Advertisements

Add Comment

📖 Read More