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

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

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

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

FuelPHP

FuelPHPは、軽量高速で開発が可能なPHPのWebアプリケーションフレームワークです。

Amazon EC2

Amazon EC2は“Amazon Elastic Compute Cloud”の略称です。Amazon Web Services(AWS)の一部であり、仮想化されたWebサーバーのコンピュータリソースをレンタルできるサービスです。

Q&A

解決済

1回答

2145閲覧

Fuelphp 1.8, php 7.2でSessionが使えない?

sumo-tori

総合スコア9

PHP

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

FuelPHP

FuelPHPは、軽量高速で開発が可能なPHPのWebアプリケーションフレームワークです。

Amazon EC2

Amazon EC2は“Amazon Elastic Compute Cloud”の略称です。Amazon Web Services(AWS)の一部であり、仮想化されたWebサーバーのコンピュータリソースをレンタルできるサービスです。

0グッド

1クリップ

投稿2018/08/22 12:39

前提・実現したいこと

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);

試したこと

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

色々と試しているうちにログを吐いてみたら、ログイン時のセッションIDがリダイレクト先でセッションから情報取得をしようとした際に、
IDが変わっていたことが確認できました。
それが原因なのかなーとは思ってますが、どう対応すればよいかわからず、、、、投稿しました。

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

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

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

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

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

guest

回答1

0

ベストアンサー

リダイレクトによってセッションが再生成されるのが原因の様に思います。

http(ログイン画面) -> https(遷移先画面) のようになっていませんでしょうか?
あるいは、遷移元と遷移先が違うサブドメインのURLとか。

投稿2018/09/03 02:29

taka-saan

総合スコア665

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

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

sumo-tori

2018/09/03 03:59

ご回答ありがとうございます! おっしゃる通り、リクエストする度に再生成されてしまっていたことが確認できました。 その上で毎回生成するのではなく、ちゃんと保存されたセッションを利用するようにするために 調査していったところ、ログイン後に明示的に `Session::start()` してないことに気づき、 その対応を行ったらログインができました!
SonHV

2024/03/15 01:51

それで、どう変わりましたか??
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問