php显示全部登录用户数_php实现单用户登录-超简单的方式

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();

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值