好湿?好紧?好多水好爽自慰,久久久噜久噜久久综合,成人做爰A片免费看黄冈,机机对机机30分钟无遮挡

主頁 > 知識庫 > Yii2框架實現登錄、退出及自動登錄功能的方法詳解

Yii2框架實現登錄、退出及自動登錄功能的方法詳解

熱門標簽:個性化地圖標注在線 怎么在高德地圖標注行走軌跡 施工地圖標注怎么做 襄陽房產電銷機器人招商 百度地圖標注飯店位置怎么 百度地圖標注名編輯 安徽移動外呼系統(tǒng) 清遠陽山400電話號碼如何申請 深圳400電話辦理那家好

本文實例講述了Yii2框架實現登錄、退出及自動登錄功能的方法。分享給大家供大家參考,具體如下:

自動登錄的原理很簡單。主要就是利用cookie來實現的

在第一次登錄的時候,如果登錄成功并且選中了下次自動登錄,那么就會把用戶的認證信息保存到cookie中,cookie的有效期為1年或者幾個月。

在下次登錄的時候先判斷cookie中是否存儲了用戶的信息,如果有則用cookie中存儲的用戶信息來登錄,

配置User組件

首先在配置文件的components中設置user組件

'user' => [
 'identityClass' => 'app\models\User',
 'enableAutoLogin' => true,
],

我們看到enableAutoLogin就是用來判斷是否要啟用自動登錄功能,這個和界面上的下次自動登錄無關。

只有在enableAutoLogin為true的情況下,如果選擇了下次自動登錄,那么就會把用戶信息存儲起來放到cookie中并設置cookie的有效期為3600*24*30秒,以用于下次登錄

現在我們來看看Yii中是怎樣實現的。

一、第一次登錄存cookie

1、login 登錄功能

public function login($identity, $duration = 0)
{
  if ($this->beforeLogin($identity, false, $duration)) {
   $this->switchIdentity($identity, $duration);
   $id = $identity->getId();
   $ip = Yii::$app->getRequest()->getUserIP();
   Yii::info("User '$id' logged in from $ip with duration $duration.", __METHOD__);
   $this->afterLogin($identity, false, $duration);
  }
  return !$this->getIsGuest();
}

在這里,就是簡單的登錄,然后執(zhí)行switchIdentity方法,設置認證信息。

2、switchIdentity設置認證信息

public function switchIdentity($identity, $duration = 0)
{
  $session = Yii::$app->getSession();
  if (!YII_ENV_TEST) {
   $session->regenerateID(true);
  }
  $this->setIdentity($identity);
  $session->remove($this->idParam);
  $session->remove($this->authTimeoutParam);
  if ($identity instanceof IdentityInterface) {
   $session->set($this->idParam, $identity->getId());
   if ($this->authTimeout !== null) {
    $session->set($this->authTimeoutParam, time() + $this->authTimeout);
   }
   if ($duration > 0  $this->enableAutoLogin) {
    $this->sendIdentityCookie($identity, $duration);
   }
  } elseif ($this->enableAutoLogin) {
   Yii::$app->getResponse()->getCookies()->remove(new Cookie($this->identityCookie));
  }
}

這個方法比較重要,在退出的時候也需要調用這個方法。

這個方法主要有三個功能

① 設置session的有效期

② 如果cookie的有效期大于0并且允許自動登錄,那么就把用戶的認證信息保存到cookie中

③ 如果允許自動登錄,刪除cookie信息。這個是用于退出的時候調用的。退出的時候傳遞進來的$identity為null

protected function sendIdentityCookie($identity, $duration)
{
  $cookie = new Cookie($this->identityCookie);
  $cookie->value = json_encode([
   $identity->getId(),
   $identity->getAuthKey(),
   $duration,
  ]);
  $cookie->expire = time() + $duration;
  Yii::$app->getResponse()->getCookies()->add($cookie);
}

存儲在cookie中的用戶信息包含有三個值:

$identity->getId()
$identity->getAuthKey()
$duration

getId()和getAuthKey()是在IdentityInterface接口中的。我們也知道在設置User組件的時候,這個User Model是必須要實現IdentityInterface接口的。所以,可以在User Model中得到前兩個值,第三值就是cookie的有效期。

二、自動從cookie登錄

從上面我們知道用戶的認證信息已經存儲到cookie中了,那么下次的時候直接從cookie里面取信息然后設置就可以了。

1、AccessControl用戶訪問控制

Yii提供了AccessControl來判斷用戶是否登錄,有了這個就不需要在每一個action里面再判斷了

public function behaviors()
{
  return [
   'access' => [
    'class' => AccessControl::className(),
    'only' => ['logout'],
    'rules' => [
     [
      'actions' => ['logout'],
      'allow' => true,
      'roles' => ['@'],
     ],
    ],
   ],
  ];
}

2、getIsGuest、getIdentity判斷是否認證用戶

isGuest是自動登錄過程中最重要的屬性。

在上面的AccessControl訪問控制里面通過IsGuest屬性來判斷是否是認證用戶,然后在getIsGuest方法里面是調用getIdentity來獲取用戶信息,如果不為空就說明是認證用戶,否則就是游客(未登錄)。

public function getIsGuest($checkSession = true)
{
  return $this->getIdentity($checkSession) === null;
}
public function getIdentity($checkSession = true)
{
  if ($this->_identity === false) {
   if ($checkSession) {
    $this->renewAuthStatus();
   } else {
    return null;
   }
  }
  return $this->_identity;
}

3、renewAuthStatus 重新生成用戶認證信息

protected function renewAuthStatus()
{
  $session = Yii::$app->getSession();
  $id = $session->getHasSessionId() || $session->getIsActive() ? $session->get($this->idParam) : null;
  if ($id === null) {
   $identity = null;
  } else {
   /** @var IdentityInterface $class */
   $class = $this->identityClass;
   $identity = $class::findIdentity($id);
  }
  $this->setIdentity($identity);
  if ($this->authTimeout !== null  $identity !== null) {
   $expire = $session->get($this->authTimeoutParam);
   if ($expire !== null  $expire  time()) {
    $this->logout(false);
   } else {
    $session->set($this->authTimeoutParam, time() + $this->authTimeout);
   }
  }
  if ($this->enableAutoLogin) {
   if ($this->getIsGuest()) {
    $this->loginByCookie();
   } elseif ($this->autoRenewCookie) {
    $this->renewIdentityCookie();
   }
  }
}

這一部分先通過session來判斷用戶,因為用戶登錄后就已經存在于session中了。然后再判斷如果是自動登錄,那么就通過cookie信息來登錄。

4、通過保存的Cookie信息來登錄 loginByCookie

protected function loginByCookie()
{
  $name = $this->identityCookie['name'];
  $value = Yii::$app->getRequest()->getCookies()->getValue($name);
  if ($value !== null) {
   $data = json_decode($value, true);
   if (count($data) === 3  isset($data[0], $data[1], $data[2])) {
    list ($id, $authKey, $duration) = $data;
    /** @var IdentityInterface $class */
    $class = $this->identityClass;
    $identity = $class::findIdentity($id);
    if ($identity !== null  $identity->validateAuthKey($authKey)) {
     if ($this->beforeLogin($identity, true, $duration)) {
      $this->switchIdentity($identity, $this->autoRenewCookie ? $duration : 0);
      $ip = Yii::$app->getRequest()->getUserIP();
      Yii::info("User '$id' logged in from $ip via cookie.", __METHOD__);
      $this->afterLogin($identity, true, $duration);
     }
    } elseif ($identity !== null) {
     Yii::warning("Invalid auth key attempted for user '$id': $authKey", __METHOD__);
    }
   }
  }
}

先讀取cookie值,然后$data = json_decode($value, true);反序列化為數組。

這個從上面的代碼可以知道要想實現自動登錄,這三個值都必須有值。另外,在User Model中還必須要實現findIdentityvalidateAuthKey這兩個方法。

登錄完成后,還可以再重新設置cookie的有效期,這樣便能一起有效下去了。

$this->switchIdentity($identity, $this->autoRenewCookie ? $duration : 0);

三、退出 logout

public function logout($destroySession = true)
{
  $identity = $this->getIdentity();
  if ($identity !== null  $this->beforeLogout($identity)) {
   $this->switchIdentity(null);
   $id = $identity->getId();
   $ip = Yii::$app->getRequest()->getUserIP();
   Yii::info("User '$id' logged out from $ip.", __METHOD__);
   if ($destroySession) {
    Yii::$app->getSession()->destroy();
   }
   $this->afterLogout($identity);
  }
  return $this->getIsGuest();
}
public function switchIdentity($identity, $duration = 0)
{
  $session = Yii::$app->getSession();
  if (!YII_ENV_TEST) {
   $session->regenerateID(true);
  }
  $this->setIdentity($identity);
  $session->remove($this->idParam);
  $session->remove($this->authTimeoutParam);
  if ($identity instanceof IdentityInterface) {
   $session->set($this->idParam, $identity->getId());
   if ($this->authTimeout !== null) {
    $session->set($this->authTimeoutParam, time() + $this->authTimeout);
   }
   if ($duration > 0  $this->enableAutoLogin) {
    $this->sendIdentityCookie($identity, $duration);
   }
  } elseif ($this->enableAutoLogin) {
   Yii::$app->getResponse()->getCookies()->remove(new Cookie($this->identityCookie));
  }
}

退出的時候先把當前的認證設置為null,然后再判斷如果是自動登錄功能則再刪除相關的cookie信息。

更多關于Yii相關內容感興趣的讀者可查看本站專題:《Yii框架入門及常用技巧總結》、《php優(yōu)秀開發(fā)框架總結》、《smarty模板入門基礎教程》、《php面向對象程序設計入門教程》、《php字符串(string)用法總結》、《php+mysql數據庫操作入門教程》及《php常見數據庫操作技巧匯總》

希望本文所述對大家基于Yii框架的PHP程序設計有所幫助。

您可能感興趣的文章:
  • Yii2.0框架模型添加/修改/刪除數據操作示例
  • Yii2.0框架模型多表關聯查詢示例
  • yii2.0框架使用 beforeAction 防非法登陸的方法分析
  • Yii2框架實現登陸添加驗證碼功能示例
  • Yii2中組件的注冊與創(chuàng)建方法
  • Yii2框架實現注冊和登錄教程
  • Yii2 中實現單點登錄的方法
  • 修改yii2.0用戶登錄使用的user表為其它的表實現方法(推薦)
  • YII2自動登錄Cookie總是失效的解決方法
  • Yii2實現多域名跨域同步登錄退出
  • Yii2中OAuth擴展及QQ互聯登錄實現方法
  • YII2框架中自定義用戶認證模型,完成登陸和注冊操作示例

標簽:駐馬店 臨夏 欽州 中衛(wèi) 延邊 黑河 南昌 阜陽

巨人網絡通訊聲明:本文標題《Yii2框架實現登錄、退出及自動登錄功能的方法詳解》,本文關鍵詞  Yii2,框架,實現,登錄,退出,;如發(fā)現本文內容存在版權問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內容系統(tǒng)采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《Yii2框架實現登錄、退出及自動登錄功能的方法詳解》相關的同類信息!
  • 本頁收集關于Yii2框架實現登錄、退出及自動登錄功能的方法詳解的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 又污又黄又爽动态图GIF| 97夜夜操| 强开小嫩苞一区二区三区l| 二次元露胸| 一级婬片A级试看26分钟 | 看看中国特级黄毛片| 老师胸乳???裸露网站| 精品国产专区91在线app| 国产丰满老厨女房乱| 久久久久久精品人妻大豆网| 蜜芽影院??在线AV| 免费a级作爱片免费观看中| 超级乱淫小黄文多男一女| 好硬~好爽~别进去~动态图| 高清????免费爱奇艺| 8050午夜免费看成人影片| 国产真实乱系列2孕妇| 免费又爽又黄禁片观看1000| 成年黄大片| 男人j进 女人p网站在线观看| 91在线无码精品秘?色| 免费污视频| 三亚私人高清影院品牌加盟免费| 亚洲精品国产精品国自产网站按摩| 曰批视频免费40分钟免费| 国产第二十三页浮力影院| 精品视频999| 男女交性全过程无遮挡会员| 国产一级毛片精品A片在线美传媒 乱码精品国产成人观看免费 | 被撑到合不拢H宫交| 国产香蕉尹人视频在线| 小芸的放荡日记高H| 男人激烈吮乳吃奶动态图十二秒| 91操操操| 粉嫩白浆小泬150P| 麻豆一二三区AV精品传媒软件| 日本xxxx18vr| gay男男免费网站视频| 亚洲sss综合天堂久久久| Chinese裸男GaYGAYS?| 小婷的稚嫩小泬H|