Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

JungleJim's avatar

Session Variable not set

Hi Guys, I'm pretty new to laravel and I've inherited a project that was created by an external developer.

The project is basically an Iframed mini-site that's used on multiple websites, it dynamically changes the content by means of a querystring parameter passed to the iframe in the url - the parameter is called 'site'

There is a HomeController setup which in turn 'hands off' to a service which has been created to handle the steps in the mini-site (follows):

public function __construct(StepsService $service, SubmissionService $bsservice) { $this->service = $service; $this->bsservice = $bsservice; }

public function index(Request $request)
{
    return $this->service->step($request);
}

}

The step 'service' handles the session variables etc. (follows).

class StepsService extends WebService { public function __construct(SubmissionService $bss) { $this->bss = $bss; }

public function step($request)
{
    if ($request->has('site')) {
        $request->session()->put('quote-site', $request->get('site'));
    }

    if ($request->session()->has('quote-site')) {
        switch ($request->session()->get('quote-site')) {
            case 1:
                return $this->step_site1($request);
            case 2:
                return $this->step_site2($request);
            default:
                return 'ERROR #1';
        }
    } else {
        return 'ERROR #2';
    }
}

}

Now comes the issue (thanks for bearing with!)...

Sporadically - we're seeing the 'ERROR #2' returned instead of the corresponding step. It looks as if the session variable 'quote-site' is not being recognised as set (if ($request->session()->has('quote-site')) {) - even though it's set on the preceding line? ($request->session()->put('quote-site', $request->get('site'));)

Any ideas would be gratefully receieved... IF you need any more info, please ask away..

thanks

James

0 likes
5 replies
JungleJim's avatar

Hi Guys,

apologies, I obviously didn't explain myself very well.

The central issue I'm experiencing (with Laravel 5.1) is that I'm unable to see a session variable set with

$request->session()->put('quote-site', $request->get('site'));

2 lines later I'm trying to check its existance with

if ($request->session()->has('quote-site')) {

In some cases - not all, it's failing to pick up the session variable that should have been set.

Abi's avatar

check your app url.

This should be set in you .env file

or take a look at

config/app.php
 &&
config/session.php

@junglejim

JungleJim's avatar

Hi Abi,

firstly, thanks for the help.

APP_URL is not set in .env, and it's left with the default 'http://localhost' in app.php

Abi's avatar

same ulr?

try this


  if ($request->has('site')) {

        $request->session()->put('quote-site', $request->get('site'),40);
    }


    dd(session()->all());
Abi's avatar

@JungleJim if that still does not work try this


Route::group(['middleware' => ['web']], function () {
/// hit this route to create the session
    Route::get('/session/create', function (\Illuminate\Http\Request $request){
      
       $request->session()->put('key', 'test',900);

    });

});

///Check these other routes to see if the session is set
Route::get('/', function () {



    dd(session()->all());

});
Route::get('/test', function (){

    dd(session()->all());

});

Route::get('/test2page', function (){

    dd(session()->all());

});

Please or to participate in this conversation.