dousi8559 2015-06-18 03:16
浏览 29
已采纳

CodeIgniter登录和会话

I am currently building a web for student registration and I am using CodeIgniter. I already managed to insert the form to my database and I am using email and password from the submitted form to login. I got a problem in my login page,somehow it cant check either the email or password exists on the database or not and it cant redirect to my desired page either.

This is my controller

public function index()
{
    $data = '';
    if($this->input->post('submit'))
    {
        $this->form_validation->set_rules('email','email','required');
        $this->form_validation->set_rules('password','password','md5|required');

        if($this->form_validation->run())
        {    
            $email = $this->input->post('email');
            $password = md5($this->input->post('password'));

            $this->load->model('testmodel','user');

            $member = $this->user->selectUser("WHERE email = '$email' AND password = '$password'");

            if($member->has_rows())
            {
                $user_data = array(
                                'name'  => $member->row('name'),
                                'nisn'  => $member->row('nisn'),
                                'email'     => $member->row('email'),
                                'logged_in_admin' => TRUE
                           );

                $this->session->set_userdata($user_data); redirect('pages/userpage/userpage');
            }else 
            {
                $data['message'] = '<div class = "error">Username and password wrong!</div>';
            }
        }else 
        {
            $data['message'] = '<div class = "error">' . validation_errors() .  '</div>';
        }
    }

    $this->load->view('pages/userlogin/userlogin',$data);
}

This is the selectuser function in testmodel

public function selectUser($where = '', $sort = 'DESC', $limit = '')
{
    $query = "
        SELECT SQL_CALC_FOUND_ROWS * FROM student_table
            $where
        ORDER BY ticketnumber $sort
            $limit
    ";
    return $this->db->query($query);
}'
  • 写回答

2条回答 默认 最新

  • doudu2404 2017-08-03 06:19
    关注

    Try this code to login with the use of session.

    public function index()
    {
      //get the posted values
      $email= $this->input->post("email");
      $password = $this->input->post("password");
    
      //set validations
      $this->form_validation->set_rules("email", "Email", "trim|required");
      $this->form_validation->set_rules("password", "Password", "trim|required");
    
      if ($this->form_validation->run() == FALSE)
      {
           //validation fails
           $this->load->view('Your_Login_View');
      }
      else
      {
           //validation succeeds
           if ($this->input->post('btn_login') == "Login")
           {
                //check if Email and password is correct
                $usr_result = $this->testmodel->selectUser($email, $password);
                if ($usr_result > 0) //active user record is present
                {
                     //set the session variables
                     $_SESSION['email'] = $email;
                     $_SESSION['loginuser'] = true;
    
                    redirect("Your_Desired_page");
                }
                else
                {
                     $this->session->set_flashdata('msg', '<div class="alert alert-danger text-center">Invalid email and password!</div>');
                     redirect('admin');
                }
           }
           else
           {
                redirect('admin');
           }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 删除虚拟显示器驱动 删除所有 Xorg 配置文件 删除显示器缓存文件 重启系统 可是依旧无法退出虚拟显示器
  • ¥15 vscode程序一直报同样的错,如何解决?
  • ¥15 关于使用unity中遇到的问题
  • ¥15 开放世界如何写线性关卡的用例(类似原神)
  • ¥15 关于并联谐振电磁感应加热
  • ¥15 this signal is connected to multiple drivers怎么解决
  • ¥60 请查询全国几个煤炭大省近十年的煤炭铁路及公路的货物周转量
  • ¥15 请帮我看看我这道c语言题到底漏了哪种情况吧!
  • ¥66 如何制作支付宝扫码跳转到发红包界面
  • ¥15 pnpm 下载element-plus