2

アクションログインでCookie認証を作成しますが、別のコントローラーまたは別のアクションで呼び出しますが、ユーザー名だけをエコーするだけです。

例えば:

class LoginController extends Controller
{
    public function actionIndex()
    {  
       if (isset($_POST['UserLogin'])) {
            $username = $_POST['UserLogin']['email'];
            $password = $_POST['UserLogin']['password'];
            $dataLogin = Users::model()->findByAttributes(array('email'=>$username, 'password'=>md5($password), 'role'=>1, 'status'=>1));
            if($dataLogin == NULL) {
               Yii::app()->user->setFlash('login_mess',Yii::t('app','Invalid email or password!'));
               $this->redirect($_SERVER['HTTP_REFERER']);
            }
            else{

                Yii::app()->session['Authentication'] = $dataLogin;

                $cookies = new CHttpCookie('authentication', $dataLogin);
                $cookies->expire = time() + 7*24*60*60;
                Yii::app()->request->cookies['authentication'] = $cookies;

                $this->redirect('/profile');
            }
       }
    }
    public function actionLogout()
    {
       echo "<pre>";
        print_r(Yii::app()->request->cookies['authentication']);die;
        echo "</pre>";
        Yii::app()->session['Authentication'] = null;
        $this->redirect('/');
    }
}

actionLogout で、そのCookieを印刷してみます:

CHttpCookie Object
(
    [name] => authentication
    [value] => Bàn Chân Trần
    [domain] => 
    [expire] => 0
    [path] => /
    [secure] => 
    [httpOnly] => 
    [_e:CComponent:private] => 
    [_m:CComponent:private] => 
)

しかし、私は期待しています:

CHttpCookie Object
(
    [name] => authentication
    [value] => Users Object
        (
            ....

            [_new:CActiveRecord:private] => 
            [_attributes:CActiveRecord:private] => Array
                (
                    [id] => 4
                    [name] => Bàn Chân Trần
                    [email] => hongquan156@gmail.com
                    [phone_code] => +44
                    [phone] => 0988888888
                    [country] => 2
                    [language] => vi
                    [password] => 
                    [role] => 1
                    [device_token] => 
                    [active_key] => 9D9T7n1oWlFiRy1EJ3ULDVlBDtw4VQWT
                    [status] => 1
                    [token] => 
                    [payment] => 0
                    [created] => 
                )

            [_related:CActiveRecord:private] => Array
                (
                )

            [_c:CActiveRecord:private] => 
            [_pk:CActiveRecord:private] => 4
            [_alias:CActiveRecord:private] => t
            [_errors:CModel:private] => Array
                (
                )

            [_validators:CModel:private] => 
            [_scenario:CModel:private] => update
            [_e:CComponent:private] => 
            [_m:CComponent:private] => 
        )

    [domain] => 
    [expire] => 1345801074
    [path] => /
    [secure] => 
    [httpOnly] => 
    [_e:CComponent:private] => 
    [_m:CComponent:private] => 
)

どうすればこの問題を解決できますか... ありがとうございます!

4

1 に答える 1

1

オブジェクトを Cookie に保存しているので、Cookie に保存する前にシリアル化し、取得後にシリアル化を解除する必要があります。

$cookies = new CHttpCookie('authentication', serialize($dataLogin));

print_r(unserialize(Yii::app()->request->cookies['authentication']));die;
于 2012-08-31T13:00:01.580 に答える