Using $_SESSION in Laravel From non-Laravel project
Asked Answered
M

4

8

In my company, we make login in one application (non-Laravel). When the login is made, we store the session info in the $_SESSION variables, like $_SESSION['XPTO'].

The user has access to a lot of tools (all non-Laravel) and we use everywhere the $_SESSION['XPTO'] to get the data that we need about the authenticated user.

The problem is that I developed a new tool with Laravel 5.3 and I need to get the data that is inside the $_SESSION variables. I think Laravel do not use the native PHP sessions!

So, how can I get that information?

Massicot answered 11/10, 2016 at 12:52 Comment(8)
May https://mcmap.net/q/1325436/-laravel-session_start-returning-a-1/39960346#39960346Carpathoukraine
You can store and get sessions in laravel like Session::put(); and Session::get() . More information here: laravel.com/docs/5.3/sessionFrore
@IlyaYaremchuk Thanks! But I need to integrate my laravel project with my login application (is a non-laravel project). The login app stores in $_SESSION['XPTO'] the username. I need to get that data in my Laravel project :xMassicot
You really shouldn't have any issues access the session variable in Laravel , you won't be able to use the built in session object from laravel (eg. Session::). Are the apps on the same domain?Cogswell
@DavidNguyen yes they are So, if I store data in the login app as $_SESSION['XPTO'], when I enter to my laravel application, should I acess that variable like Session::get('XPTO') correct?Massicot
@Erbi no you access the variables using $_SESSION['XPTO'] the Laravel session does not use the native php sessionCogswell
@DavidNguyen ok. I understand that Laravel does not use the native php session. But is there a way to access in the laravel project that variable that I stored in the non-laravel project? I am sorry for boring you again, but I am a little confusedMassicot
@ErbiSilva can't think of any easy way to do vice versa however you could set up routes for laravel to do some helper functions to get your old app working.Cogswell
M
8

to access $_SESSION, simply place session_start() at the beginning of the index.php file of your laravel-project

Mide answered 5/3, 2018 at 14:54 Comment(1)
TY... laravel's built in sessions were being glitchy / not working as expected. $_SESSION['whatever'] ?? 'default' working fine now.Orchardist
M
4

Laravel doesn't use native PHP sessions since Laravel 5.

We are no longer using Symfony's (and therefore PHP's) session handling facilities, and are using a custom solution that is simpler and easier to maintain

In Laravel you want to use Session:: facade or session() global helper to work with sessions:

// Saving value.
session()->put('key', 'value');

// Gettinng value.
session('key');
Micromillimeter answered 11/10, 2016 at 12:59 Comment(1)
but with that, how can I acess the data that comes from my login application?Massicot
O
2

Laravel provides more inbuilt method to get and set session data. it’s easy to working with session in laravel.A session variable is used to store some information or some data about user or anything you want to access on all pages of an application.In laravel session configuration is stored in "app/config/session.php".

I have found here a very easy tutorial to understand the usage of the SESSION in laravel which you can also find it in easy for learning.

Setting a single variable in session :-

Below is the syntax of the Session

Syntax :- Session::put('key', 'value');

Example :-

Session::put('email', $data['email']); //array index
Session::put('email', $email); // a single variable
Session::put('email', '[email protected]'); // a string

Retrieving value from session :-

The syntax for retrieving the values from the session

Syntax :- Session::get('key');

Example:

Session::get('email');

Checking a variable exist in session :-

// Checking email key exist in session.
if (Session::has('email')) {
  echo Session::get('email');
}

Deleting a variable from session :-

syntax :- Session::forget('key');

Example:

Session::forget('email');

Removing all variables from session :-

Session::flush();
Orazio answered 11/10, 2016 at 13:5 Comment(2)
thanks for the answer I have an application that is old. And its responsible to do a Login. After I enter with my login, inside that application I have to choose wich tool I want to use. MyPortal is a new tool. My first tool developed with laravel! I need to access $_SESSION in laravel to get the username and some information more from the login application. My answer is, if I can get that in Laravel. Because with that Session::class I have no success yet :xMassicot
@ErbiSilva Did you find a solution for this issue? sorry for disturbing after many yrs. I am seeking for a solution for the same issue theses daysBeaudoin
J
0

you just have to use

session_start();

in your laravel code

and then

dd($_SESSION);

//it will print all keys inside sessions variable in your laravel project..
Jolynjolynn answered 18/6, 2022 at 12:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.