夺命雷公狗ThinkPHP项目之----商城7后台登录控制以及注销

如果我们的网站是考虑到上线的话,那么后台肯定会做限制的,不是谁想进入后台就让他们进入,所以我们要对后台做些手脚不让外人登录进来

首先创建一个CommonController的控制器,然后在后台的每个控制器下都继承CommonController控制器这样即可起到防范的作用。

代码如下:

<?php
    namespace AdminController;
    use ThinkController;
    header("Content-Type:text/html;charset=utf-8");
    class CommonController extends Controller{
        //通过初始化方法去解决用户翻墙问题
        public function _initialize(){
            if(!session('?admin')){
                $this -> redirect("Login/login",array(),3,'请登录后再进入');
            }
        }
    }

Index控制器下的继承的示例代码如下:

<?php
namespace AdminController;
use ThinkController;
class IndexController extends CommonController {
    public function index(){
       $this -> display();
      }

      public function top(){
          $this -> display();
      }

      public function menu(){
          $this -> display();
      }

      public function drag(){
          $this -> display();
      }

      public function main(){
          $this -> display();
      }
}

刚才我们们的中间层里面定义了Loginlogin的跳转页面(登录页),那么下一步我们就开始写登录页面了。

创建一个LoginController.class.php的控制器,主要是用于登录的,注意继承的时候别继承错误了,他不是继承中间层的,而是继承Controller的。

视图下的模版到时候我会分享出来,后台登录界面里面的验证码也是thinkphp里面自带的,但是里面的代码太长了,记不了,其实这是很正常的,没必要去记,在手册上的---专题---验证码---生成验证码,里面就有了,而且在有写时候验证码看不清,点击下他会动的更改代码的,其实也很简单的,就是在验证码里加了个id然后再用js控制下即可达到更换的效果,如下代码所示:

        //生成验证码
        public function code(){
            //创建验证码类的实例
            $verify = new ThinkVerify();
            //清空ob缓存
            ob_clean();
            //设置初始化验证码
            $verify -> codeSet = "0123456789";
            $verify -> length = "4";
            $verify -> fontSize = "30px";
            $verify -> fonttf = "4.ttf";
            $verify -> useCurve = false;
            $verify -> useNoise = false;
            
            //显示验证码,并且保存在session中
            $verify -> entry();
            //如果感觉不好看可以通过tplxThinkPHPLibraryThinkVerify.class.php里面进行设置
        }

login.html的代码如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="__ADMIN__/styles/general.css" rel="stylesheet" type="text/css" />
<link href="__ADMIN__/styles/main.css" rel="stylesheet" type="text/css" />
<style type="text/css">
body {
  color: white;
}
</style>
<script>
  window.onload = function(){
    document.getElementById("code").onclick = function(){
      this.src = "__URL__/code/_/"+new Date().getTime();
    }
  }
</script>
</head>
<body style="background: #278296">
<form method="post" action="__URL__/Login/" name='theForm'>
  <table cellspacing="0" cellpadding="0" style="margin-top: 100px" align="center">
  <tr>
    <td><img src="__ADMIN__/images/login.png" width="178" height="256" border="0" alt="ECSHOP" /></td>
    <td style="padding-left: 50px">
      <table>
      <tr>
        <td>管理员姓名:</td>
        <td><input type="text" name="username" /></td>
      </tr>
      <tr>
        <td>管理员密码:</td>
        <td><input type="password" name="password" /></td>
      </tr>
      <tr>
        <td>验证码:</td>
        <td><input type="text" name="captcha" class="capital" /></td>
      </tr>
      <tr>
      <td colspan="2" align="right"><img id="code" src="__CONTROLLER__/code" width="145" height="20" alt="CAPTCHA" border="1" onclick= this.src="index.php?act=captcha&"+Math.random() style="cursor: pointer;" title="看不清?点击更换另一个验证码。" />
      </td>
      </tr>
      <tr><td colspan="2"><input type="checkbox" value="1" name="remember" id="remember" /><label for="remember">请保存我这次的登录信息</label></td></tr>
      <tr><td>&nbsp;</td><td><input type="submit" value="进入管理中心" class="button" /></td></tr>
      <tr>
        <td colspan="2" align="right">&raquo; <a href="../" style="color:white">返回首页</a> &raquo; <a href="get_password.php?act=forget_pwd" style="color:white">你忘记了密码吗?</a></td>
      </tr>
      </table>
    </td>
  </tr>
  </table>
  <input type="hidden" name="act" value="signin" />
</form>

</body>

在login.html模版下的src其实指向的是控制器下的code方法而已,而且点击验证码时候换图片主要是由这段代码来进行实现的。

<script>
  window.onload = function(){
    document.getElementById("code").onclick = function(){
      this.src = "__URL__/code/_/"+new Date().getTime();
    }
  }
</script>

下一步就是来定义管理员登录的login方法了

代码如下所示

//管理员登录
        public function login(){
            header("Content-Type:text/html;charset=utf-8");
            if(IS_POST){
                //获取验证码,帐号和密码
                $username = I('username');
                $password = I('password');
                $code = I('code');
                //先验证验证码
                $verify = new ThinkVerify();   
                if(!$verify->check($code)){
                    $this -> error('验证码错误');
                }

                //验证用户名,调用模型来完成
                if(D('admin')->checkUser($username,$password)){
                    $this -> success('登录成功',U('Index/index'),1);
                    var_dump($_SESSION);die;
                }else{
                    $this -> error('用户名或者密码错误');
                }
                return;    
            }
            //载入登录页面
            $this -> display();
        }

这里面的帐号和密码是通过model层里面进行验证的,在shoppShopAdminModel里面创建一个AdminModel.class.php的模型层,代码如下所示:

<?php
    namespace AdminModel;
    use ThinkModel;
    //后台管理员模型
    class AdminModel extends Model{
        //验证用户名和密码
        public function checkUser($username,$password){
            $condition['admin_name'] = $username; //数据库设计的时候这里是admin_name
            $condition['password'] = md5($password);  //数据库设计的时候这里是password
            if($admin=$this->where($condition)->find()){
                //成功,保存session,并且跳转到首页
                session('admin',$admin);
                return true;
            }else{
                return false;
            }
        }
    }

注销这个就简单多了,直接让session(null)就可以了,如下代码所示

<?php
    namespace AdminController;
    use ThinkController;
    //登录控制器
    class LoginController extends Controller{
        //管理员登录
        public function login(){
            header("Content-Type:text/html;charset=utf-8");
            if(IS_POST){
                //获取验证码,帐号和密码
                $username = I('username');
                $password = I('password');
                $code = I('code');
                //先验证验证码
                $verify = new ThinkVerify();   
                if(!$verify->check($code)){
                    $this -> error('验证码错误');
                }

                //验证用户名,调用模型来完成
                if(D('admin')->checkUser($username,$password)){
                    $this -> success('登录成功',U('Index/index'),1);
                }else{
                    $this -> error('用户名或者密码错误');
                }
                return;    
            }
            //载入登录页面
            $this -> display();
        }

        //生成验证码
        public function code(){
            //创建验证码类的实例
            $verify = new ThinkVerify();
            //清空ob缓存
            ob_clean();
            //设置初始化验证码
            $verify -> codeSet = "0123456789";
            $verify -> length = "4";
            $verify -> fontSize = "30px";
            $verify -> fonttf = "4.ttf";
            $verify -> useCurve = false;
            $verify -> useNoise = false;
            
            //显示验证码,并且保存在session中
            $verify -> entry();
            //如果感觉不好看可以通过tplxThinkPHPLibraryThinkVerify.class.php里面进行设置
        }


        //注销
        public function logout(){
            session(null);
            $this -> success('恭喜成功退出','Login/login',3);
        }

    }

不过要在模板页的top里面修改以下a标签如下所示:

<a href="__MODULE__/Login/logout" target="_top" class="fix-submenu">退出</a>
原文地址:https://www.cnblogs.com/leigood/p/4944035.html