質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.31%
Laravel

LaravelとはTaylor Otwellによって開発された、オープンソースなPHPフレームワークです。Laravelはシンプルで表現的なシンタックスを持ち合わせており、ウェブアプリケーション開発の手助けをしてくれます。

Q&A

解決済

1回答

968閲覧

セッションタイムアウトとなったsession_id を取得したい

paseri457

総合スコア2

Laravel

LaravelとはTaylor Otwellによって開発された、オープンソースなPHPフレームワークです。Laravelはシンプルで表現的なシンタックスを持ち合わせており、ウェブアプリケーション開発の手助けをしてくれます。

0グッド

0クリップ

投稿2023/09/10 06:19

編集2023/09/10 07:12

実現したいこと

セッションの有効期限が切れてリクエストされたセッションのsessionidを取得したい。
(もしかして、ブラウザ側でsessionの有効期限を判定してクッキー情報からlaravel_sessionを削除するので、有効期限が切れたセッションidは取得できないですか?)

前提

laravelのmiddlewareに標準で存在するsessionが生きているかの判定について調査中。

以下ファイルが大きく関わっている可能性。
特にSessionGuard.phpにてリクエストされたセッションidが有効であるかを判定していると考えています。
vendor/laravel/framework/src/Illuminate/Auth/Middleware/Authenticate.php
vendor/laravel/framework/src/Illuminate/Auth/GuardHelpers.php
vendor/laravel/framework/src/Illuminate/Auth/SessionGuard.php

発生している問題・エラーメッセージ

取得できるsession_idが、セッションタイムアウト前に生成されたsession_idではなく
既に新しく生成されたsession_idとなっていた。

該当のソースコード

vendor/laravel/framework/src/Illuminate/Auth/Middleware/Authenticate.php protected function authenticate($request, array $guards) { if (empty($guards)) { $guards = [null]; } foreach ($guards as $guard) { if ($this->auth->guard($guard)->check()) { return $this->auth->shouldUse($guard); } } $this->unauthenticated($request, $guards); }
vendor/laravel/framework/src/Illuminate/Auth/GuardHelpers.php /** * Determine if the current user is authenticated. * * @return bool */ public function check() { return ! is_null($this->user()); }
vendor/laravel/framework/src/Illuminate/Auth/SessionGuard.php /** * Get the currently authenticated user. * * @return \Illuminate\Contracts\Auth\Authenticatable|null */ public function user() { if ($this->loggedOut) { return; } // If we've already retrieved the user for the current request we can just // return it back immediately. We do not want to fetch the user data on // every call to this method because that would be tremendously slow. if (! is_null($this->user)) { return $this->user; } $id = $this->session->get($this->getName()); // First we will try to load the user using the identifier in the session if // one exists. Otherwise we will check for a "remember me" cookie in this // request, and if one exists, attempt to retrieve the user using that. if (! is_null($id) && $this->user = $this->provider->retrieveById($id)) { $this->fireAuthenticatedEvent($this->user); } // If the user is null, but we decrypt a "recaller" cookie we can attempt to // pull the user data on that cookie which serves as a remember cookie on // the application. Once we have a user we can return it to the caller. if (is_null($this->user) && ! is_null($recaller = $this->recaller())) { $this->user = $this->userFromRecaller($recaller); if ($this->user) { $this->updateSession($this->user->getAuthIdentifier()); $this->fireLoginEvent($this->user, true); } } return $this->user; }

試したこと

上記3ファイルにてリクエストされたセッションidをログに出力する処理を実施。
・セッションタイムアウト前のセッションidを取得し、手元にメモを残す。
・セッションタイムアウト後のセッションidを取得し、比較する。

あくまでもリクエストされるセッションidはlaravelで設定しているタイムアウト時間に関わらず、ブラウザが保持しているsession_idがリクエストされると想定していた。
リクエストされたsession_idがlaravelで設定した有効期限より上回っている場合に
laravel側で新しくセッションidを生成し、ブラウザ側にセッションidをレスポンスで渡すと考えていた。
ただ、今回の結果ではそもそもリクエストされているsession_idが新しく生成されたものとなっていた。
ブラウザ側でlaravelが設定した有効期限を検知して、有効期限が切れている場合に新たにセッションidを生成してサーバに送信する挙動はないと認識しているので、期待している結果と異なっている。

補足情報(FW/ツールのバージョンなど)

laravel 9
session.php にて'lifetime' => env('SESSION_LIFETIME', 1 ) を設定
(テストの為、1分でセッションが切れるように。)

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

laravel_sessionの判定しているのは別のミドルウエアで
src/vendor/laravel/framework/src/Illuminate/Session/Middleware/StartSession.php
になると思います。
StartSession.phpのgetSessionLifetimeInSeconds()であなたがenvで1分に設定したlifetimeも読み込んでいます。
ブラウザのデベロッパーツールでCookieを確認するとlaravel_sessionが存在してExpireが1分後に設定されています。
1分を過ぎてリクエストしたときのCookieにはlaravel_sessionは期限切れで存在しないのでLaravel側ではlaravel_sessionがないので新しく作る処理をします。

src/vendor/laravel/framework/src/Illuminate/Session/Store.php

1 /** 2 * Set the session ID. 3 * 4 * @param string|null $id 5 * @return void 6 */ 7 public function setId($id) 8 { 9 $this->id = $this->isValidId($id) ? $id : $this->generateSessionId(); 10 }

vendor/laravel/framework/src/Illuminate/Auth/Middleware/Authenticate.php
vendor/laravel/framework/src/Illuminate/Auth/GuardHelpers.php
vendor/laravel/framework/src/Illuminate/Auth/SessionGuard.php

この3つはファイルセッションの後のログインセッションに関するソースだと思います。

投稿2023/09/10 13:21

niiyz

総合スコア131

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

paseri457

2023/09/11 00:23

ご回答ありがとうございます。 .envで設定した、セッションの有効期限がクッキー側にexpire(自分が確認した時はmax-ageでした。)として渡される。 クッキーは、expireを超過した情報の有効期限が切れると自動的に削除するため、ブラウザからリクエストを送る際に情報として送信されない。 ということでよろしいでしょうか。
niiyz

2023/09/11 02:53

Google Chromeで確認している前提です。 デベロッパーツールのアプリケーション-ストレージ-Cookieで見るとわかりにくいと思うので ネットワークタブでsession idを確認しているURLを選択すれば「リクエスト Cookie」と「レスポンス Cookie」に分かれて表示されるのでわかりやすいと思います。
paseri457

2023/09/11 03:14

ありがとうございます。 確認したところ、リクエスト cookieには値が何も入っていなかったです。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.31%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問