Home >Backend Development >PHP Tutorial >yii登录如何记住用户名?

yii登录如何记住用户名?

WBOY
WBOYOriginal
2016-06-06 20:30:241168browse

在使用yii登录的时候,我将第二个参数$duration设置成86400,既将用户的登录信息保存到cookie里面24小时。

<code>Yii::app()->user->login($identity,86400);
</code>

当我想要解析这个cookie的时候,发现cookie是这样的。

<code>array(4) {
  ["CNZZDATA2724999"]=>
  string(48) "cnzz_eid=1338694115-1438177009-&ntime=1438182411"
  ["Hm_lvt_598eaf80391e655d10ebe38419b36fb2"]=>
  string(32) "1438310921,1438410705,1438589813"
  ["PHPSESSID"]=>
  string(26) "8ak62atemv5t4vc67hjrj1l705"
  ["abed3dd2114943c0bf392b4ae87e177b"]=>
  string(139) "d6991a57d52238c7b0b10d36c07f1fd9b715532ea:4:{i:0;s:2:"27";i:1;s:5:"endui";i:2;i:86400;i:3;a:2:{s:4:"role";s:1:"1";s:4:"name";s:5:"endui";}}"
}
</code>

但是我该怎么才可以解析这个cookie呢?是用什么方法?

回复内容:

在使用yii登录的时候,我将第二个参数$duration设置成86400,既将用户的登录信息保存到cookie里面24小时。

<code>Yii::app()->user->login($identity,86400);
</code>

当我想要解析这个cookie的时候,发现cookie是这样的。

<code>array(4) {
  ["CNZZDATA2724999"]=>
  string(48) "cnzz_eid=1338694115-1438177009-&ntime=1438182411"
  ["Hm_lvt_598eaf80391e655d10ebe38419b36fb2"]=>
  string(32) "1438310921,1438410705,1438589813"
  ["PHPSESSID"]=>
  string(26) "8ak62atemv5t4vc67hjrj1l705"
  ["abed3dd2114943c0bf392b4ae87e177b"]=>
  string(139) "d6991a57d52238c7b0b10d36c07f1fd9b715532ea:4:{i:0;s:2:"27";i:1;s:5:"endui";i:2;i:86400;i:3;a:2:{s:4:"role";s:1:"1";s:4:"name";s:5:"endui";}}"
}
</code>

但是我该怎么才可以解析这个cookie呢?是用什么方法?

<code>// cookies
$cookie = array(
    'CNZZDATA2724999' => 'cnzz_eid=1338694115-1438177009-&ntime=1438182411',
    'Hm_lvt_598eaf80391e655d10ebe38419b36fb2' => '1438310921,1438410705,1438589813',
    'PHPSESSID' => '8ak62atemv5t4vc67hjrj1l705',
    'abed3dd2114943c0bf392b4ae87e177b' => 'd6991a57d52238c7b0b10d36c07f1fd9b715532ea:4:{i:0;s:2:"27";i:1;s:5:"endui";i:2;i:86400;i:3;a:2:{s:4:"role";s:1:"1";s:4:"name";s:5:"endui";}}' 
);
//正则
$regExp = '/(a:\d+:{.+)$/';
</code>
<code>/*假设明确知道是哪个键存储的cookie值*/
$cookieUserInfo = end($cookie);
$getUserInfoStr = preg_match($regExp,$cookieUserInfo,$matchedStr);
$getUserInfo = $matchedStr[1] ? unserialize($matchedStr[1]) : array();
print_r($getUserInfo);
</code>
<code>/*如果是随机某键某位置存储*/
$getUserInfo = array();
foreach($cookie as $cookieUserInfo){
    if(preg_match($regExp,$cookieUserInfo,$matchedStr)){
        $getUserInfo = unserialize($matchedStr[1]);
        break;
    }
}
print_r($getUserInfo);
</code>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:appserv问题Next article:php为什么array()!=0