TimoPHP做一个简单的后台登录、登出功能


登录控制器

  1. 登录控制器直接继承Timo\Core\Controller
  2. 其它需要登录后才能操作的控制器,继承自定义的公共控制器Admin,如Index控制器
  3. 通过AdminModel->checkAdmin($username, $password, $verify) 来验证登录
  4. 登录成功之后跳转到index/index
  1. namespace app\admin\controller;
  2. use app\admin\model\AdminModel;
  3. use Timo\Captcha;
  4. use Timo\Core\Controller;
  5. use Timo\Core\Request;
  6. use Timo\Core\Session;
  7. class Login extends Controller
  8. {
  9. /**
  10. * @var AdminModel
  11. */
  12. protected $AdminModel;
  13. public function __construct()
  14. {
  15. parent::__construct();
  16. $this->AdminModel = new AdminModel();
  17. }
  18. /**
  19. * 登录
  20. */
  21. public function index()
  22. {
  23. if (Request::isPost()) {
  24. $username = Request::post('username', '');
  25. $password = Request::post('password', '');
  26. $verify = Request::post('verify', '');
  27. $check_ret = $this->AdminModel->checkAdmin($username, $password, $verify);
  28. if ($check_ret['code'] == 1) {
  29. Session::delete('v_login');
  30. $this->redirect('index/index');
  31. }
  32. $this->assign('msg', $check_ret['msg']);
  33. }
  34. $this->view->set('layer_on', false);
  35. $this->display();
  36. }
  37. /**
  38. * 退出
  39. */
  40. public function logout()
  41. {
  42. Session::destroy();
  43. return $this->success('退出成功', 'login/index');
  44. }
  45. /**
  46. * 生成验证码
  47. */
  48. function verify()
  49. {
  50. $Captcha = new Captcha();
  51. $code = $Captcha->getCode();
  52. Session::set('v_login', $code);
  53. $Captcha->getImage();
  54. }
  55. }

验证是否是管理员,并登录

  1. 验证成功之后,将管理员信息存入session
  2. Session::set('u.uid', $admin_info['id']);
  3. Session::set('u.name', $admin_info['username']);
  1. namespace app\admin\model;
  2. use Timo\Core\Model;
  3. use Timo\Core\Session;
  4. use Timo\Core\App;
  5. class AdminModel extends Model
  6. {
  7. function __construct($dbType = '', $dbName = '')
  8. {
  9. parent::__construct($dbType, $dbName);
  10. $this->setTablePrefix('back_');
  11. }
  12. /**
  13. * 检测是否是管理员
  14. *
  15. * @param $username
  16. * @param $password
  17. * @param string $verify_code 验证码
  18. * @param bool $is_verify 是否验证验证码
  19. * @return array|string
  20. */
  21. public function checkAdmin($username, $password, $verify_code = '', $is_verify = true)
  22. {
  23. $admin_info = $this->getRow(array('username' => $username), 'id, username, password');
  24. if (!$admin_info) {
  25. return App::result(4001, '没有该用户');
  26. }
  27. if ($is_verify && $verify_code != Session::get('v_login')) {
  28. return App::result(4002, '验证码错误');
  29. }
  30. $password = sha1(md5($password));
  31. if ($admin_info['password'] == $password) {
  32. Session::set('u.uid', $admin_info['id']);
  33. Session::set('u.name', $admin_info['username']);
  34. return App::result(1, '登录成功');
  35. }
  36. return App::result(4003, '密码错误');
  37. }
  38. }

后台公共控制器Admin

  1. 检测是否登录
  2. Admin控制器在初始化的时候会检测是否已登录,没登录,将跳转到登录页面login/index
  1. namespace app\admin\controller;
  2. use Timo\Core\Controller;
  3. use Timo\Core\Session;
  4. class Admin extends Controller
  5. {
  6. /**
  7. * 管理员信息['uid' => 1, 'name' => 'admin']
  8. *
  9. * @var mixed
  10. */
  11. protected $u;
  12. public function __construct()
  13. {
  14. parent::__construct();
  15. $this->u = Session::get('u');
  16. if (!empty($this->u)) {
  17. $this->assign('u', $this->u);
  18. } else {
  19. $this->redirect('login/index');
  20. }
  21. }
  22. }

其它需要登录后才能操作的控制器

  1. 这些控制器需继承Admin控制器
  2. 这里只是以Index控制器来说明
  1. <?php
  2. namespace app\admin\controller;
  3. class Index extends Admin
  4. {
  5. public function index()
  6. {
  7. $this->display();
  8. }
  9. }