zoukankan      html  css  js  c++  java
  • yii2.0用户登录,退出判断(摘录)

    文章来源:http://blog.sina.com.cn/s/blog_88a65c1b0101ix13.html

    判断用户是否登录

    Yii2.0 里面,判断用户是否已经登录,我们用下面的代码即可

    Yii::$app->user->isGuest;

    示例:如果用户已经登录,直接调用 goHome() 方法

    if (!Yii::$app->user->isGuest) {

    return $this->goHome();

    }

    获取登录用户名

    在 yii2.0 里面,获取登录状态下的用户名称,可以用下面的代码。

    Yii::$app->user->identity->username;

    用户退出操作我们用下面的方法

    $this->user->logout();

    使用示例:

    public function actionLogout()

    {

    Yii::$app->user->logout();

    return $this->goHome();

    }

    下面是 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();
    }

    我生活的地方,我为何要生活。
  • 相关阅读:
    POJ 3177 Redundant Paths(无向图缩点)
    POJ 1502 MPI Maelstrom
    LightOJ 1094
    POJ 1564 Sum It Up(深搜)
    C语言复习6_doWhile循环
    进阶学习
    C语言复习5_调试
    C语言复习4_while循环
    C语言复习3_条件结构
    C语言复习2_运算符
  • 原文地址:https://www.cnblogs.com/hsd1727728211/p/5858933.html
Copyright © 2011-2022 走看看