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

adrian7's avatar

What's the best way to start the session manually?

What would be the best way to start the session manually (e.g. if some conditions are met) instead of having Laravel/Lumen starting it automatically when the middleware is enabled?

0 likes
14 replies
bestmomo's avatar

It's what I do to start a session manually :

$id = $app['encrypter']->decrypt($_COOKIE[$app['config']['session.cookie']]);
$app['session']->driver()->setId($id);
$app['session']->driver()->start();
adrian7's avatar

@bestmomo where would you put that? in app.php?

Also is there a cleaner approach, maybe extending the StartSession middleware?

bestmomo's avatar

Yes not so clean but usefull when use Laravel App from external code ;)

For your case you can create a StartSession middleware for your application that extends the Illuminate one and only override handle method to manage as you want.

1 like
adrian7's avatar

I did just that but now the session vars do not persist...

<?php

namespace App\Http\Middleware;

use App\Client;
use \Closure;
use Illuminate\Http\Request;

class StartSession extends \Illuminate\Session\Middleware\StartSession{

    /**
     * Session timeout, in seconds. A new session id will be generated after TIMEOUT;
     */
    const TIMEOUT = 10;

    /**
     * The client connected to session
     * @var \App\Client;
     */
    protected $client;

    protected function setClient(Client $client){
        $this->client = $client;
    }

    protected function getClient(){
        return $this->client;
    }
    public function handle($request, Closure $next){

        $token      = $request->query->get('token') ?: $request->headers->get('X-TOKEN');
        $validToken = false; //TODO validate token

        if( $token and $validToken ){

            //valid session token
            return parent::handle($request, $next);

        }
        else{
        
            $validSignature  = true; //TODO validate request sig

            if( $validSignature ){
                return parent::handle($request, $next);
            }
            else{
                throw new \Exception('Invalid signature!');
            }

        }

    }

}
bestmomo's avatar

How do you declare your middleware in kernel ?

adrian7's avatar
$app->middleware([
    App\Http\Middleware\StartSession::class,
...
bestmomo's avatar

I think you need Cookie middlewares too before it.

adrian7's avatar

Updated to

$app->middleware([
    Illuminate\Cookie\Middleware\EncryptCookies::class,
    Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
    App\Http\Middleware\StartSession::class,
    //Illuminate\Session\Middleware\StartSession::class,
    Illuminate\View\Middleware\ShareErrorsFromSession::class,
    Laravel\Lumen\Http\Middleware\VerifyCsrfToken::class,
]);

But still have the same issue. Simply session vars set using my class do not persist... . Can't see the reason why?

bestmomo's avatar

I think you should also call the parent constructor to have the manager working.

jlrdw's avatar

Isn't session auto started in Laravel and couldn't regenerate session do the trick?

adrian7's avatar

@jlrdw if I comment all of the middleware I get a session via PHP but no cookies are sent to browser. I guess in this case session falls back to the array driver.

BTW, anyone managed to reproduce the issue?

Please or to participate in this conversation.