2015-06-18 117 views
0

我目前正在建立一個網絡學生註冊,我使用的是CodeIgniter。我已經設法將表單插入到我的數據庫中,並且我使用提交表單中的電子郵件和密碼進行登錄。我的登錄頁面出現問題,無法檢查數據庫中是否存在電子郵件或密碼,也無法重定向到所需的頁面。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); 
}' 
+1

您收到的錯誤是什麼? –

+0

您是否有從您的代碼獨立測試的工作數據庫連接? – DrCord

+0

theres沒有錯誤,它只是無法重定向到我想要的網頁,是的,它是 –

回答

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


} 
+0

抱歉,遲到的迴應,好吧我今晚會嘗試這個....感謝之前 –

+0

我得到了另一個問題,每當我提交它只重定向到管理頁面不是我想要的頁面,雖然我已經插入了正確的電子郵件和密碼..任何線索? –

+0

這意味着您的代碼每次都會在其他部分跳轉,確保您的提交按鈕具有name =「btn_login」和value =「Login – Numair

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