A
A
asdasdqwe2021-12-19 13:29:56
PHP
asdasdqwe, 2021-12-19 13:29:56

How do sessions work in laravel?

We can work with sessions like this: But if we try to get data as usual, we get an error If we start the session, everything works, BUT the session is empty!!
$request->session()->get('key');

var_dump($_SESSION); // undefined variable

session_start();
var_dump($_SESSION);    // array[]


How so? _previous _token etc. should be stored there as well.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
FanatPHP, 2021-12-19
@asdasdqwe

there is nothing magical about the session.
it's just a cookie in the browser and storage on the server, where an array with information is searched for by the key from the session.
that's all.
to realize such hands - business for 5 minutes.
what Lara does.
session () climbs into cookies and takes the key from there. using this key, climbs into the storage (it can be any - base, radish, etc.), gets an array from it, and from the array gets 'key'
everything is simple
, only two more or less complicated moments
1. the key in the cookie is necessary write not anyhow what but so that it was impossible to pick up. random_bytes to help, and more authentic, 20 pieces.
2. In order not to climb into the storage every time you need to write a value to the session, you can define a function that is called after the script is executed. and in it already write an array from the script to the storage

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question