PHP_THINKPHP_study11_后台验证与自动运行的方法

一、登陆后台验证

   1、登陆html配置提交地址:

    

 <form action="{:U('Admin/Login/login')}" method="post" id="login">

   2、Admin/LoginAction.class.php添加login()

    

Public function  login()
    {
        if (!IS_POST) halt('页面不存在');
//        echo $_SESSION['verify'] . '<br/>';
//        echo md5($_POST['code']);
//        p($_POST);
        if (I('code', '', 'md5') != session('verify')) {
            $this->error('验证码错误');
        }
        $username = I('username');
        $pwd = I('password', '', 'md5');
        $user = M('user')->where(array('username' => $username))->find();
        if (!$user || $user['password'] != $pwd) {
            $this->error('账号或者密码错误');
        }
        if ($user['lock']) $this->error('用户被锁定');
        $data = array(
            'id' => $user['id'],
            'logintime' => time(),
            'loginip' => get_client_ip()
        );
        M('user')->save($data);

        session('uid', $user['id']);
        session('username', $user['username']);
        session('logintime', date('Y-m-d H:i:s'), $user['logintime']);
        session('loginip', $user['loginip']);

        $this->redirect('Admin/Index/index');
    }

  3、为了校验session信息判断登陆跳转

    写一个CommonAction.class.php 重写 _initialize()方法

    

<?php

Class CommonAction extends Action
{
    Public function  _initialize()
    {
        if (!isset($_SESSION['uid']) || !isset($_SESSION['username'])) {//如果没有检测到session中的uid或者username让他跳转到登陆页
            $this->redirect('Admin/Login/index');
        }
    }
}
?>

  4、IndexAction.class.php 继承CommonAction

    这样首页跳转就会初始化判断是否通过登陆过来的而不是url跳转过来

   

  5、写个退出方法在IndexAction.class.php

   

   public function logout()
    {
        session_unset();//清除session
        session_destroy();//摧毁session
        $this->redirect('Admin/Login/index');
    }

 6、Index.html中

   

  <a href="{:U('Admin/Index/logout')}" target="_self">退出</a>

 

详情请看源码

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值