Laravel 5 doesn't store session variable between pages [duplicate]

Asked
Active3 hr before
Viewed126 times

7 Answers

90%

I have a brand new installation of Laravel 5, in fact I have tried this on multiple versions and keep hitting the same issue.,I have not changed anything from the default except setting the session driver to redis. (File based also has the same issue).,Every time I visit the page though, it recreates a new session. ,It might be worth noting I am using a homestead vm (completely stock) to test this. I have also tried using multiple browsers. No cookies are ever sent to the browser, I presume a session id should be sent to the browser as part of the initial get request?

If you open up app/Http/Kernel.php, you'll see that the StartSession middleware is part of a middleware group called web. You need to put all your routes inside there for your example to work.

Route::group(['middleware' => ['web']], function() {
   Route::get('/set/{value}', function($value) {
      var_dump(Session::getId());
      Session::set('test', $value);
      return view('welcome');
   });

   Route::get('/get', function() {
      return 'Get '.Session::get('test');
   });
});
88%

The session.auto_start configuration directive ,session_id() - Get and/or set the current session id,Example #4 Reading the session and closing it, session_​status

load more v
72%

database - sessions will be stored in a database used by your application., Setup Installation Configuration Homestead ,When using the database session driver, you will need to setup a table to contain the session items. Below is an example Schema declaration for the table:,Application Structure

The session may be accessed in several ways, via the HTTP request's session method, the Session facade, or the session helper function. When the session helper is called without arguments, it will return the entire session object. For example:

session() - > regenerate();
load more v
65%

$value = Session::get('variableSetOnPageA');
load more v
75%

Database Driver & Version: Redis,Since Laravel persists the entire session data array at the end every request, in the case of concurrent requests, any changed session data is very likely to be lost.,@taylorotwell Is this not a security issue since the default auth guard stores the login information in the session?,Thanks, this is good solution for authenticated users. For guests, however, we still have to store references(cart id at least) in session and it might get destroyed and thus the user cart would be lost.

    | -- -- -- --Request A(changes session variable) -- -- -- -- |
       |
       -- -- -- -- -- --Request B(no changes to session data) -- -- -- -- -- -- |
load more v
40%

Remember this is just to test while you're learning, so make sure to delete it from your application afterward!,There are a couple of things you'll need to do to get your development environment setup for your new Laravel project.,Of course, in a real scenario, you'd want to make sure you validate all information coming in from a form, but for a demo application, this is fine. ,Virtualbox to set up your environment (latest version)

You should have the following extensions enabled in your php.ini file:

extension = curl
extension = fileinfo
extension = gd2
extension = mbstring
extension = openssl
load more v
22%

Coupling with the web framework: When using server-based authentication we are tied to our framework’s authentication scheme. It is really hard, or even impossible, to share session data between different web frameworks written in different programming languages., Coupling with the web framework: When using server-based authentication we are tied to our framework’s authentication scheme. It is really hard, or even impossible, to share session data between different web frameworks written in different programming languages. ,It is important to note that we want to keep a web token as small as possible, so use only necessary data inside public and private claims.,Require the barryvdh/laravel-cors package in our composer.json and update our dependencies.

Here is a JWT token example:

eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.
eyJpc3MiOiJ0b3B0YWwuY29tIiwiZXhwIjoxNDI2NDIwODAwLCJodHRwOi8vdG9wdGFsLmNvbS9qd3RfY2xhaW1zL2lzX2FkbWluIjp0cnVlLCJjb21wYW55IjoiVG9wdGFsIiwiYXdlc29tZSI6dHJ1ZX0.
yRQYnWzskCZUxPwaQupWkiUzKELZ49eM7oWxAQK_ZXw
load more v

Other "undefined-undefined" queries related to "Laravel 5 doesn't store session variable between pages [duplicate]"