CodeIgniter登录和会话

时间:2015-06-18 03:16:18

标签: php codeigniter session login

我目前正在为学生注册构建一个网站,我正在使用CodeIgniter。我已经设法将表单插入我的数据库,我使用提交的表单中的电子邮件和密码登录。我的登录页面出现了问题,无论如何都无法检查数据库中是否存在电子邮件或密码,也无法重定向到我想要的页面。

这是我的控制器

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);
}

这是testmodel中的selectuser函数

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 个答案:

答案 0 :(得分:1)

尝试这样你的控制器:

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');
           }
      }
 }

你的模特:

function selectUser($email, $password)
 {
      $this->db->select('*');
      $this->db->from('student_table');
      $this->db->where('email',$email);
      $this->db->where('password',$password);
      $query = $this->db->get();

      return $query->num_rows();


 }

答案 1 :(得分:0)

尝试使用此代码登录会话。

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');
       }
    }
}