后端开发记录登录态

#后端开发记录登录态思路

	public function login(){
		if(登录成功){
		    $time = 30*24*60*60;
            $data['sid'] = session_id();
            $key = 'ses_'.$data['sid'];
            $this->openid = $this->user['openId'];
            $user_json = json_encode($data);//存入的用户数据
            $this->cache->redis->save($key,$user_json,$time);//判断登录成功将用户信息写入redis
		}
	}

/**
除了登录登录外,其他业务逻辑控制器所继承的构造函数,用于判断用户请求时是否处于登录态
**/
	public function __construct()
    {
        parent::__construct();
        $this->sid = $this->input->get_request_header('sid',TRUE);//客户端请求时header头传参
        if(!$this->sid)
        {
            $this->render(0,'参数错误');
        }
        $key = 'ses_'.$this->sid;
        $this->user = json_decode($this->cache->redis->get($key),TRUE);
        if($this->user)
        {
            $this->openid = $this->user['openId'];
        }
        else
        {
            $this->render(-1,'未登录');
        }
    }

猜你喜欢

转载自blog.csdn.net/weixin_41722647/article/details/88851820