美文网首页
php实现单用户登录-超简单的方式

php实现单用户登录-超简单的方式

作者: leptune | 来源:发表于2020-09-20 16:12 被阅读0次
    public function index() {
        if (request()->isPost()) {
            $phone = input('post.phone');
            $passwd = input('post.passwd');

            $teacher = db('teacher')->where(['passwd' => md5($passwd), 'phone' => $phone])->find();
            if (!$teacher) {
                return json(['error' => 1, 'msg' => '用户名或密码错误!']);
            }

            $cache = \think\Cache::init();
            $redis = $cache->handler();
            if (!$redis) {
                return json(['error' => 1111, 'msg' => '系统错误!']);
            }
            $nid = session_id();
            session_commit();
            // echo 'new->',$nid.PHP_EOL;

            // 登录之前,退出所有其他终端
            while ($session_id_to_destroy = $redis->rPop(config('cache.prefix').'tea_sessions_'.$teacher['id'])) {
                // hijack then destroy session specified.
                session_id($session_id_to_destroy);
                // echo 'del->',$session_id_to_destroy.PHP_EOL;
                session_start();
                session_destroy();
                session_commit();
            }
            session_id($nid);
            session_start();
            // echo 'new->',session_id().PHP_EOL;die;
            session('web.teacher', $teacher);
            // 将登录成功的最新的会话id存到redis,以支持单点登录
            $redis->rPush(config('cache.prefix').'tea_sessions_'.$teacher['id'], session_id());
            // dump(session('web.teacher'));die;
            return json(['error' => 0, 'msg' => '登录成功!']);
        }
        return $this->fetch();
    }

相关文章

网友评论

      本文标题:php实现单用户登录-超简单的方式

      本文链接:https://www.haomeiwen.com/subject/tgcpyktx.html