我在操作登录中创建了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] =>
)
我怎样才能解决这个问题...谢谢!
答案 0 :(得分:1)
由于您正在将对象存储到cookie中,因此在将其存储到cookie之前对其进行序列化,并在检索它之后对其进行反序列化:
$cookies = new CHttpCookie('authentication', serialize($dataLogin));
print_r(unserialize(Yii::app()->request->cookies['authentication']));die;