zoukankan      html  css  js  c++  java
  • thinkphp5登录并保存session、根据不同用户权限跳转不同页面

    本文讲述如何在thinkphp5中完成登录并保存session、然后根据不同的用户权限跳转相应页面功能的实现。我也在学习thinkphp源码的路上,记录一下并与大家分享。完成该步骤主要有以下三个步骤完成。


    一、密码校验

    这里view层提交过来的用户名和密码是不加密的,数据中的密码是经过md5加密的,所以首先对密码进行加密,然后跟数据库中的记录比对,如果一致则认为成功。


    二、session保存

    如果校验成功则将用户信息保存在session中。


    三、根据不同权限跳转

    有时候我们对于不同的用户展示的页面也不同,这时就需要我们根据用户的权限跳转到相应的页面。


    四、实现代码

     1 // 登录
     2 public function login()
     3 {
     4     //密码加密并从数据库查找记录
     5     $map['username'] = input('post.a');
     6     $map['password'] = md5(input('post.b'));
     7     $user=db('user')->where($where)->find();
     8     //验证成功则保存session
     9     if ($user) {
    10         unset($user["psd"]);
    11         session("user", $user['id']);
    12         //根据不同权限跳转
    13         if($user['quanxian'] == 0){
    14             $this->redirect('Module1/index/index');
    15         }
    16         elseif ($user['quanxian'] == 1) {
    17           $this->redirect('MOdule2/index/index');
    18         }
    19         else{
    20           $this->redirect('Module3/index/index');
    21         }
    22     }else{
    23         print_r ('error!');
    24         return false;
    25     }
    26 }
  • 相关阅读:
    设计模式——装饰模式(Decorator Pattern)
    设计模式——策略模式(Strategy Pattern)
    设计模式——简单工厂模式(SimpleFactory Pattern)
    入博客园三周年记
    android+Service
    surfaceView+canvas+paint+bitmap
    Enable Sublime text 2 to support GBK in Mac
    androidstudio+opencv
    mac下的环境变量PATH
    Curl命令
  • 原文地址:https://www.cnblogs.com/woleyia/p/10127142.html
Copyright © 2011-2022 走看看