Index.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. defined('BASEPATH') OR exit('No direct script access allowed');
  3. /**
  4. * Class 登录类
  5. */
  6. class Index extends Public_Controller {
  7. function __construct(){
  8. parent::__construct();
  9. $this->load->helper(array('form','date'));
  10. $this->load->model("user_model");
  11. }
  12. /**
  13. * 登录页面
  14. */
  15. public function login(){
  16. if($this->session->user_id){
  17. redirect(base_url()."main/index");
  18. }
  19. $this->display("index/login.html");
  20. }
  21. /**
  22. * 检查登录
  23. */
  24. public function check_login(){
  25. if($this->input->post()){
  26. $message = "";
  27. $username = $this->input->post("username");
  28. $password = $this->input->post("password");
  29. if($username != "" && $password !="") {
  30. $user = $this->user_model->get_user_with_username_password($username, hash_hmac('sha256',$password,$this->config->item('secret_key')));
  31. if (count($user) >= 1) {
  32. if ($user[0]['status'] == "20") {
  33. $message = "该帐号未验证,请验证后再登录!";
  34. } elseif ($user[0]['status'] == "30") {
  35. $message = "该帐号已锁定,请联系管理员!";
  36. } elseif ($user[0]['status'] == "40") {
  37. $message = "该帐号已停用,请联系管理员!";
  38. } else {
  39. $rand = substr(md5(microtime()), rand(0, 26), 5);
  40. $session_data = array("user_id" => (string)$user[0]["user_id"], "current_key" => $rand, "username" => $user[0]['username'], "user_type" => $user[0]['user_type']);
  41. $user[0]['last_login_time'] = new MongoDB\BSON\UTCDateTime(time()*1000);
  42. $this->user_model->update_user($user[0]);
  43. $this->session->set_userdata($session_data);
  44. }
  45. } else {
  46. $message = "帐号名或密码错误,请重试!";
  47. }
  48. }else{
  49. $message = "用户名或密码不能为空!";
  50. }
  51. if($message != "") {
  52. $this->response(array("msg" => $message));
  53. }else{
  54. $this->response(array("url" => "main/index"));
  55. }
  56. } else{
  57. $message = "参数错误,请重试!";
  58. $this->response(array("msg"=>$message));
  59. }
  60. }
  61. /**
  62. * 登出
  63. */
  64. public function logout(){
  65. if($this->session->user_id){
  66. $this->session->unset_userdata(array("user_id","current_key","username","user_type","from"));
  67. }
  68. redirect("login");
  69. }
  70. }