CakePHPでログイン機能の実装をしているのですが、ローカル環境では動作するのですが、本番環境だとログインできません。本番環境にはGMOのiClusta+のレギュラープランを使用しております。
本番環境での失敗の原因としまして、ログイン時にlogin関数内の$this->Auth->login()がtrueになっていないことにあります。
さらに掘り下げるとlib/Model/Datasource/CakeSession.phpのcheck関数にて
self::_hasSession()やself::start()がローカルであればtrueになるところが共にfalseになっています。また、セッションもデータベースに保存されません。ログにも何も出力されません。
$this->Session->write('hoge', 'fuga');を実行して$this->Session->read('hoge')
で確認したところ値を読み込め、$_SESSIONを使用しても値は取得できます。
また、セッション以外のものについてはCakePHPのsave関数でデータベースに値を書き込めることも確認しています。
・ローカル環境
Windows Version: Windows 7 Professional SP1 32-bit
XAMPP Version: 5.6.12
Control Panel Version: 3.2.1
mysql Ver 14.14 Distrib 5.6.26, for Win32 (x86)
データベース
mysql
1mysql> show full columns from `cake_sessions`; 2+---------+--------------+-----------------+------+-----+---------+-------+----- 3----------------------------+---------+ 4| Field | Type | Collation | Null | Key | Default | Extra | Priv 5ileges | Comment | 6+---------+--------------+-----------------+------+-----+---------+-------+----- 7----------------------------+---------+ 8| id | varchar(255) | utf8_general_ci | NO | PRI | | | sele 9ct,insert,update,references | | 10| data | text | utf8_general_ci | NO | | NULL | | sele 11ct,insert,update,references | | 12| expires | int(11) | NULL | YES | | NULL | | sele 13ct,insert,update,references | | 14+---------+--------------+-----------------+------+-----+---------+-------+----- 15----------------------------+---------+ 163 rows in set (0.01 sec)
core.php中では以下の設定を行っています。
php
1Configure::write('Session', array( 2 'defaults' => 'database', 3 'cookie' => 'SID', 4 'timeout' => 259200, 5 'ini' => Array( 6 'session.cookie_lifetime' => 2580000, 7 'session.gc_maxlifetime' => 2580000, 8 'session.gc_probability' => 1, 9 'session.gc_divisor' => 100 10 ) 11 ));
ログイン画面を実装しているUsersControllerは以下の通りとなります。
php
1public $components = array( 2 'Session', 3 'Auth' => array( 4 'loginAction' => array( 5 'controller' => 'users', 6 'action' => 'login' 7 ), 8 'loginRedirect' => array( 9 'controller' => 'members', 10 'action' => 'index' 11 ), 12 'logoutRedirect' => array( 13 'controller' => 'mains', 14 'action' => 'index', 15 'home' 16 ), 17 'authenticate' => array( 18 'Form' => array( 19 'passwordHasher' => 'Blowfish', 20 'fields' => array( 21 'username' => 'mail_address', 22 'password' => 'password') 23 ) 24 ) 25 ), 26 'Mail', 'Render' 27 );
php
1public function login() { 2 if ($this->request->is('post')) { 3 if ($this->Auth->login()) { 4 $this->Render->redirect($this, 'members', 'index'); 5 } 6 else { 7 $this->Session->setFlash('登録情報と一致しません。', 'default', array(), 'auth'); 8 $this->Render->redirect($this, 'mains', 'complete/failure'); 9 } 10 } 11 12 $this->Render->redirect($this, 'mains', 'index'); 13 }
宜しくお願い致します。