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();
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
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?
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();
@bestmomo where would you put that? in app.php?
Also is there a cleaner approach, maybe extending the StartSession middleware?
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.
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!');
}
}
}
}
How do you declare your middleware in kernel ?
$app->middleware([
App\Http\Middleware\StartSession::class,
...
I think you need Cookie middlewares too before it.
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?
I think you should also call the parent constructor to have the manager working.
Tried that too...
Isn't session auto started in Laravel and couldn't regenerate session do the trick?
@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?
I'm also having an issue with this. If there is an existing Session, Laravel is happy to ::put() and ::get() on it. But if there is not Session yet, it won't create one. I need to access Laravel via a regular route first to create the session, set the cookie, then I can do Session operations. Until then, no dice. :/
Here's the stack overflow:
http://stackoverflow.com/questions/35709705/using-laravel-5-sessions-externally
This should solve your problem:
Please or to participate in this conversation.