From f8a6540d045137736bba8b9d92e25407dacb68d0 Mon Sep 17 00:00:00 2001 From: klg Date: Thu, 25 Mar 2021 17:38:38 +0100 Subject: [PATCH 1/4] Fix usage of ini_set --- src/UrlSession.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/UrlSession.php b/src/UrlSession.php index fe14d64..f4340bf 100644 --- a/src/UrlSession.php +++ b/src/UrlSession.php @@ -15,9 +15,6 @@ class UrlSession */ public function handle($request, Closure $next) { - ini_set('session.use_trans_sid', 1); - ini_set('session.use_cookies', 0); - ini_set('session.use_only_cookies', 0); return $next($request); } } From 14aecb7301a00fb6aa1957e296e6454048105fb7 Mon Sep 17 00:00:00 2001 From: klg Date: Thu, 25 Mar 2021 17:41:31 +0100 Subject: [PATCH 2/4] Refactor appending session url to check if the route we want to go to has the middleware --- README.md | 5 +++-- src/UrlGeneratorService.php | 20 ++++++++++++-------- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 90fd558..842cf3e 100644 --- a/README.md +++ b/README.md @@ -20,12 +20,13 @@ Installation 2. In your `config/app.php` at `providers` replace `'Illuminate\Session\SessionServiceProvider'` with `\iMi\LaravelTransSid\SessionServiceProvider::class` 3. Add `\iMi\LaravelTransSid\UrlServiceProvider::class` at the end of the providers array -4. In your `app/Http/Kernel.php` add `'urlsession' => \iMi\LaravelTransSid\UrlSession::class` to the `$routeMiddleware` array. +4. (optional) In your `app/Http/Kernel.php` add `'urlsession' => \iMi\LaravelTransSid\UrlSession::class` to the `$routeMiddleware` array. Usage ----- -To use SessionIDs in URLs add the middleware `urlsession` to your route or routegroup. +To use SessionIDs in URLs add the middleware `urlsession` (if you registered the middleware globally) or add the +`\iMi\LaravelTransSid\UrlSession::class` class directly to your route or routegroup. URLs generated with Laravel's URL function (for example `URL::to()`) will now have a session ID appended. diff --git a/src/UrlGeneratorService.php b/src/UrlGeneratorService.php index 88e391a..18ee5e1 100644 --- a/src/UrlGeneratorService.php +++ b/src/UrlGeneratorService.php @@ -7,16 +7,20 @@ class UrlGeneratorService extends UrlGenerator { - public function addSid($url) + public function addSid($url, ?\Illuminate\Routing\Route $route = null) { // Only apply transsid to routes/routegroups with the urlsession middleware. - if (in_array('urlsession', Route::current()->computedMiddleware)) { - if (strpos($url, \Config::get('session.cookie')) !== false) { - return $url; - } - $sep = (strpos($url, '?') !== false) ? '&' : '?'; - $url .= $sep . \Config::get('session.cookie') . '=' . \Session::getId(); + if ($route === null || !in_array(UrlSession::class, $route->getAction('middleware'))) { + return $url; } + + if (strpos($url, \Config::get('session.cookie')) !== false) { + return $url; + } + + $sep = (strpos($url, '?') !== false) ? '&' : '?'; + $url .= $sep . \Config::get('session.cookie') . '=' . \Session::getId(); + return $url; } @@ -39,7 +43,7 @@ public function toRoute($route, $parameters, $absolute) } $url = parent::toRoute($route, $parameters, $absolute); - $url = $this->addSid($url); + $url = $this->addSid($url, $route); return $url; } From f58dc3c4692c2176eebdb2641d85eec90c5acf8a Mon Sep 17 00:00:00 2001 From: klg Date: Fri, 26 Mar 2021 11:50:01 +0100 Subject: [PATCH 3/4] Add explanatory comment for empty middleware --- src/UrlSession.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/UrlSession.php b/src/UrlSession.php index f4340bf..907ea65 100644 --- a/src/UrlSession.php +++ b/src/UrlSession.php @@ -15,6 +15,7 @@ class UrlSession */ public function handle($request, Closure $next) { + // intentionally left empty - just for marking the requests return $next($request); } } From 0e182b0b81b8b2ea0de1a1057594714e7cb1762c Mon Sep 17 00:00:00 2001 From: klg Date: Fri, 26 Mar 2021 11:51:18 +0100 Subject: [PATCH 4/4] Use full name for $sep --- src/UrlGeneratorService.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/UrlGeneratorService.php b/src/UrlGeneratorService.php index 18ee5e1..2bf7899 100644 --- a/src/UrlGeneratorService.php +++ b/src/UrlGeneratorService.php @@ -18,8 +18,8 @@ public function addSid($url, ?\Illuminate\Routing\Route $route = null) return $url; } - $sep = (strpos($url, '?') !== false) ? '&' : '?'; - $url .= $sep . \Config::get('session.cookie') . '=' . \Session::getId(); + $separator = (strpos($url, '?') !== false) ? '&' : '?'; + $url .= $separator . \Config::get('session.cookie') . '=' . \Session::getId(); return $url; }