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?

8
  • May http://stackoverflow.com/questions/39960209/laravel-session-start-returning-a-1/39960346#39960346 Commented Oct 11, 2016 at 12:55
  • You can store and get sessions in laravel like Session::put(); and Session::get() . More information here: laravel.com/docs/5.3/session Commented Oct 11, 2016 at 12:56
  • @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 :x Commented Oct 11, 2016 at 13:44
  • 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? Commented Oct 11, 2016 at 13:56
  • @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? Commented Oct 11, 2016 at 14:20

4 Answers 4

8

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

Sign up to request clarification or add additional context in comments.

1 Comment

TY... laravel's built in sessions were being glitchy / not working as expected. $_SESSION['whatever'] ?? 'default' working fine now.
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');

1 Comment

but with that, how can I acess the data that comes from my login application?
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();

2 Comments

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 :x
@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 days
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..

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.