How do I use the same session in the Standard for all subdomains?

Asked

Viewed 317 times

0

I’m looking to log in to a users.meudominio.com subdomain. To test the session, I’m just displaying what I type in the login field on the screen with the code in the controller:

if($request->session()->has('session_login'))
   return view("welcome", ["session_login" => $request->session()->get('session_login')]);

After that a simple {{ $session_login }} displays correctly what I typed. But I also want to use this same session in the Clinic.domain.com subdomain, but it’s not working. I put the following code in the controller of this second subdomain:

if($request->session()->has('session_login'))
    View::share('session_login', $request->session()->get('session_login'));
else
    View::share('session_login', "BBB");   

But it always displays only "BBB" in the second subdomain. I’ve already configured the file Sessions.php in both applications, putting 'domain' => ".i9technology.com.br", and I’m also using Homestead. What’s missing for this session to work in both subdomains?

  • Friend I think this article will help you: https://medium.com/@pagcosma/multi-subdomain-Sessions-in-Laravel-40fd31b199fa

2 answers

0

According to that answer SOEN, just modify the file config/session.php

Look for the following line:

  'domain' => env('SESSION_DOMAIN', null),

Then put .i9technology.com.br.

A note in the reply says you need to clear browser cookies for changes to take effect.

Also remember to clear the settings cache:

php artisan config:clear

-1

  • Capture the ID session Session::getId() in Domain A
  • Send the ID captured via HTTP POST for Domain B
  • Receive this value in Domain B $sessionid_from_domainA = \Input::get('ID');
  • Set in session ID in Domain B Session::setId($sessionid_from_domainA)
  • Finally, Session::start() in Domain B
  • It didn’t work, it continues the same way, but I made some changes. I first changed the two Session.php, where it had 'domain' => ".domain.com.br" got 'domain' => env('SESSION_DOMAIN', null),, because I realized that the way it was before the session did not work even in the domain A itself. Second, I made all these changes (I took the session id, passed via GET, used setId and start in domain B) but the param session_login was not passed. I printed Session::getId() in the B domain and actually printed the right id (which came via GET), but Session::get('session_login') did not pass.

  • At least in the code you posted, you’re not setting anything in the session to session_login, Can you first set something or post the location where you’re doing it? (in the codex posted you just check if exste and send to view, then try to use in the domain B)

  • another detail, the excerpt 'domain' => env('SESSION_DOMAIN', null), makes the application look for the value SESSION_DOMAIN in your file .env, located at the root of your project, it will continue fixed in the same way. Still, I don’t think it’s affecting anything in what we’re trying to do!

  • These are the three lines of code I’m using to store in the session: $session_id = Session::getId();$request->session()->put('session_login', $request->login); return redirect("http://clinic.i9technology.com.br?session_id=$session_id");. And this is the controller code you receive in Domain B: $session_id = $request->session_id;Session::setId($session_id);Session::start();if($request->session()->has('session_login'))
 View::share('session_login', $request->session()->get('session_login')); else View::share('session_login', "AAA");return view("index");. On screen only printa AAA

  • And ta setando normal in the session, because on the page Welcome in domain A is displayed normally the session_login. But when I step to B it is not, even passing the session id and receiving with the above code. The same if is used in domain A, but gives true and falls into the IF. I have also tried to put a View::share('session_login', Session::get('session_login')) but nothing was imprinted.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.