前提・実現したいこと
PHPやFWのバージョンも古くなってきたので、バージョンアップを行っているのですが、
下記のような環境でSessionを利用してログインユーザの情報を各ページで使いまわしたいと思ってます。
- Amazon Linux
- PHP7.2
- Fuelphp1.8
- MySQL
発生している問題・エラーメッセージ
ログイン画面にてIDとパスワードを入力し、DB上のユーザ情報と照合して、ログイン成功した場合にSessionへ
該当ユーザの情報をセットし、別の画面にて同じ情報を取得して利用したいのですが、これまで検証した結果、別画面へ遷移した際にセッションが消えてしまいます。。。
該当のソースコード
PHP
1# ログイン時のセッションへのセットの仕方 2$user = \Model\User::login($account, $password); 3 4if ($user === false) { 5 return false; 6} 7 8$login_user = [ 9 'user_id' => $user['id'] 10 ,'user_name' => $user['user_name'] 11 ,'mail_address' => $user['mail_address'] 12); 13Session::set('login_user', $login_user);
PHP
1# セッションへのセット後の処理 ※リダイレクトしてます。 2Response::redirect($acces_url);
PHP
1# リダイレクト先の処理 2// セッションの取得 3$login_user = Session::get('login_user', null); 4// セッションが存在しない場合は未ログイン 5if( $login_user === null ){ 6 // Log::debug("セッションが存在しない場合は未ログイン"); 7 return false; 8}
PHP
1# fuel/app/config/session.php 2return array( 3 /** 4 * global configuration 5 */ 6 7 // set it to false to prevent the default session from being automatically created and started when accessing the 8 // Session class. Note that if you no, your session may expire prematurely as it is no longer automatically updated 9 // on every page load when you (auto) load the Session class! 10 'auto_initialize' => true, 11 12 // set it to false to prevent manually created session instances from being autostarted when they are created 13 'auto_start' => true, 14 15 // if no session type is requested, use the default 16 'driver' => 'redis', 17 18 // check for an IP address match after loading the cookie (optional, default = false) 19 'match_ip' => false, 20 21 // check for a user agent match after loading the cookie (optional, default = true) 22 'match_ua' => true, 23 24 // cookie domain (optional, default = '') 25 'cookie_domain' => '', 26 27 // cookie path (optional, default = '/') 28 'cookie_path' => '/', 29 30 // cookie http_only flag (optional, default = use the cookie class default) 31 'cookie_http_only' => null, 32 33 // whether or not to encrypt the session cookie (optional, default is true) 34 'encrypt_cookie' => true, 35 36 // if true, the session expires when the browser is closed (optional, default = false) 37 'expire_on_close' => false, 38 39 // session expiration time, <= 0 means 2 years! (optional, default = 2 hours) 40 'expiration_time' => 7200, 41 42 // session ID rotation time (optional, default = 300) Set to false to disable rotation 43 'rotation_time' => 300, 44 45 // default ID for flash variables (optional, default = 'flash') 46 'flash_id' => 'flash', 47 48 // if false, expire flash values only after it's used (optional, default = true) 49 'flash_auto_expire' => false, 50 51 // if true, a get_flash() automatically expires the flash data 52 'flash_expire_after_get' => true, 53 // for requests that don't support cookies (i.e. flash), use this POST variable to pass the cookie to the session driver 54 'post_cookie_name' => '', 55 56 // for requests in which you don't want to use cookies, use an HTTP header by this name to pass the cookie to the session driver 57 'http_header_name' => 'Session-Id', 58 59 // if false, no cookie will be added to the response send back to the client 60 'enable_cookie' => true, 61 62 // if true, session data will be synced with PHP's native $_SESSION, to allow easier integration of third-party components 63// 'native_emulation' => false, 64 'native_emulation' => true, 65 66 /** 67 * specific driver configurations. to override a global setting, just add it to the driver config with a different value 68 */ 69 70 // special configuration settings for cookie based sessions 71 'cookie' => array( 72 'cookie_name' => 'fuelcid', // name of the session cookie for cookie based sessions 73 ), 74 75 // specific configuration settings for file based sessions 76 'file' => array( 77 'cookie_name' => 'fuelfid', // name of the session cookie for file based sessions 78 'path' => '/tmp', // path where the session files should be stored 79 'gc_probability' => 5, // probability % (between 0 and 100) for garbage collection 80 ), 81 82 // specific configuration settings for memcached based sessions 83 'memcached' => array( 84 'cookie_name' => 'fuelmid', // name of the session cookie for memcached based sessions 85 'servers' => array( // array of servers and portnumbers that run the memcached service 86 'default' => array('host' => '127.0.0.1', 'port' => 11211, 'weight' => 100), 87 ), 88 ), 89 90 // specific configuration settings for database based sessions 91 'db' => array( 92 'cookie_name' => 'fueldid', // name of the session cookie for database based sessions 93 'database' => null, // name of the database name (as configured in config/db.php) 94 'table' => 'sessions', // name of the sessions table 95 'gc_probability' => 5, // probability % (between 0 and 100) for garbage collection 96 ), 97 98 // specific configuration settings for redis based sessions 99 'redis' => array( 100 'cookie_name' => 'fuelrid', // name of the session cookie for redis based sessions 101 'database' => 'default', // name of the redis database to use (as configured in config/db.php) 102 ), 103);
試したこと
- 下記の記事やFuelphp1.8のドキュメントを読んで、Session::write()など試したが、そもそもそんなFunctionがなかった・・・
- FuelPHPのSession::set_flash()のあとにredirect処理をするとセッションのデータが消える – エコテキブログ
- php - Why is session data lost after redirect in fuelPHP? - Stack Overflow
- Sessionのドライバを
file
にしたりもしてみたがダメでした。 - 下記のような記事もあるので、できるようになってるんじゃないかとは思うのですが・・・
- reCatnap: fuelPHPでセッションが使えない » PCスキルの小技・忘却防止メモ
fuel/app/config/session.php
を作成して設定値をいじって何度か試しましたが、これもダメでした。
補足情報(FW/ツールのバージョンなど)
色々と試しているうちにログを吐いてみたら、ログイン時のセッションIDがリダイレクト先でセッションから情報取得をしようとした際に、
IDが変わっていたことが確認できました。
それが原因なのかなーとは思ってますが、どう対応すればよいかわからず、、、、投稿しました。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/09/03 03:59
2024/03/15 01:51