LoginSignup
6
11

More than 5 years have passed since last update.

FuelPHPのセッション管理

Posted at

環境

FuelPHP:1.7.3

概要

FuelPHPでセッションを管理する方法がいつくつかあるのでメモしておく。FuelPHPは、デフォルトの場合、セッションをCookieで管理している。ちなみに、今回はあまり関係してこないが、FuelPHPは、PHPのセッション機能を使用せず独自の方法でセッションを管理している(Cookieに保存するセッションデータは暗号化されている)。

事前準備

「fuel/app/config」内に「session.php」が無い場合、「fuel/core/config/session.php」からコピーしてくる。

Cookie以外のセッション管理方法

Fileで管理

fuel/app/config/session.php
    // expire sooner because it's not updated when it's not used. note that auto-initializing always loads the default driver
    'auto_initialize'   => true,

    // if no session type is requested, use the default
-   'driver'            => 'cookie',
+   'driver'            => 'file',

    // check for an IP address match after loading the cookie (optional, default = false)
    'match_ip'          => false,

上記のように変更することでセッションがファイルで管理される。

DBで管理

fuel/app/config/session.php
    // expire sooner because it's not updated when it's not used. note that auto-initializing always loads the default driver
    'auto_initialize'   => true,

    // if no session type is requested, use the default
-   'driver'            => 'cookie',
+   'driver'            => 'db',

    // check for an IP address match after loading the cookie (optional, default = false)
    'match_ip'          => false,

ファイルを上記のように変更し、

ターミナル.app
php oil r session:create

上記コマンドで、セッションを管理するためのテーブルを作成する。
以上でセッションがDBで管理されるようになる。

補足

ターミナル.app
php oil r session:create

上記コマンドで生成されるテーブル定義は以下の通り。

CREATE TABLE `sessions` (
  `session_id` varchar(40) NOT NULL,
  `previous_id` varchar(40) NOT NULL,
  `user_agent` text NOT NULL,
  `ip_hash` char(32) NOT NULL,
  `created` int(10) unsigned NOT NULL,
  `updated` int(10) unsigned NOT NULL,
  `payload` longtext NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
6
11
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
11