2017-06-17 107 views
0

在這裏我想用用戶名「admin」和密碼「管理員」 與表單驗證並將這些值在會話 登錄,但在數據庫檢查它是沒有返回任何值? 我的控制器,驗證用戶名和密碼是我有一個登錄問題的用戶名和密碼是正確的

<?php 
defined('BASEPATH') OR exit('No direct script access allowed'); 

class Login extends CI_Controller 
{ 

    function __construct() 
    { 
     parent::__construct(); 
     $this->load->model('loginModel'); 
     $this->load->helper('string'); 
     $this->load->library("form_validation"); 
    } 

    //login 
    public function index() 
    { 
     $this->load->view('login'); 
    } 


    Public Function authentication()   
    { 
     $username=$this->input->post('username'); 
     $password=$this->input->post('password');   

     $this->form_validation->set_rules("username","Username","required"); 
     $this->form_validation->set_rules("password","Password","required"); 

     if($this->form_validation->run() == FALSE) 
     { 
      echo "Error"; 
      $this->session->set_flashdata('error','Invalid User Name or PASSWORD!'); 
      redirect(site_url().'login'); 
     } else { 
      $login_data = array(
           "username"=>$username, 
           "password"=>$password 
           ); 
      $authentication_response = $this->loginModel->user_authentication($login_data); 

      if($authentication_response) 
      { 
       $username = $authentication_response['UserName']; 
       $UserTypeId = $authentication_response['UserTypeId']; 
       //put the user info on session 
       $this->session->set_userdata("username", $username); 
       $this->session->set_userdata("UserTypeId", $UserTypeId); 

       if($UserTypeId == "1") 
       { 
        redirect("dashboard"); 
       } 
       if($UserTypeId == "2") 
       { 
        redirect("dashboard"); 
       } 

       if($UserTypeId == "3") 
       { 
        redirect("userPanel"); 
       } 

      } else { 
       $this->session->set_flashdata("error", "Invalid User!!! Please check your user name or password correctly"); 
       redirect("login"); 
      } 
     } 
    } 

    function logout(){ 
     $this->session->sess_destroy(); 
     redirect(site_url().'login'); 
    } 
}//end of class 
?> 

我使用SQL Server 2008中 用戶名= admin密碼=管理員USERTYPEID = 1

+0

其中loginModel-> user_authentication($ login_data)函數? – Gopalakrishnan

+0

請提供您的loginModel代碼進行審查。 –

+0

<?PHP的 類LoginModel延伸CI_Model { \t公共函數user_authentication($ login_data) \t { \t \t $ result_set = $這個 - > DB-> get_where( 「Tbl_UserList」,$ login_data); \t \t如果($ result_set-> NUM_ROWS()> 0) \t \t { \t \t \t返回$ result_set-> row_array(); \t \t} \t \t其他 \t \t { \t \t \t返回FALSE; \t \t} \t} } ?> – cks

回答

0

你應該試試這個,我已經改進了一些代碼並在我的本地服務器上進行了測試。它工作正常。希望它能解決你的問題。在這裏,我假設你的模型工作正常。

public function authentication() { 
    $this->form_validation->set_rules('user_name', 'User Name', 'required|trim'); 
    $this->form_validation->set_rules('user_password', 'Password', 'required'); 


    if ($this->form_validation->run() == TRUE) { 

     $user_name = $this->input->post('user_name'); 
     $user_password = $this->input->post('user_password'); 

     $this->load->model('loginModel'); 
     $authentication_response = $this->loginModel->user_authentication($user_name, $user_password); 


     if ($authentication_response == TRUE) { 

      $session_data = array(
       'UserTypeId' => $authentication_response['UserTypeId'], 
       'username ' => $authentication_response['username']      
      ); 

      $this->session->set_userdata('user_data', $session_data);    
      log_message('debug', 'This User Data Added To the Session: '. print_r($session_data, TRUE)); 

      if($UserTypeId == "1") 
      { 
       return redirect("dashboard"); 
      } 
      if($UserTypeId == "2") 
      { 
       return redirect("dashboard"); 
      } 

      if($UserTypeId == "3") 
      { 
       return redirect("userPanel"); 
      } 

     } else { 
      $this->session->set_flashdata('login_failed', 'Invalid Username/Password.'); 
      log_message('error', 'This User Failed To log in: '. print_r($user_name, TRUE)); 
      return redirect('your_login_form'); 
     } 
    } else { 
     //Failed. 
     log_message('error', 'Failed Login attempt Due to validation Error : '.print_r($user_name, TRUE)); 
     $this->load->view('your_login_form'); 
     // echo validation_errors(); 
    } 
} 
+0

可以請你加模型的代碼? – 2017-06-17 15:34:47

-2

你可能不返回從模型的響應。再次檢查模型或發佈模型代碼。

+0

我想我已經從模型中返回了值,但是當我回顯它的值時什麼也沒有顯示。 這裏是模型 <?PHP 類LoginModel延伸CI_Model { \t公共函數user_authentication($ login_data) \t { \t \t $ result_set = $這個 - > DB-> get_where( 「Tbl_UserList」,$ login_data); \t \t如果($ result_set-> NUM_ROWS()> 0) \t \t { \t \t \t返回$ result_set-> row_array(); \t \t} \t \t其他 \t \t { \t \t \t返回FALSE; \t \t} \t}} > – cks

+0

嘗試'的var_dump($ authentication_response);?'' 死;'剛過'$ authentication_response = $這個 - > loginModel-> user_authentication($ login_data);你這話是什麼得到。 –

+0

兄弟顯示: bool(false) 我當時正在使用xampp服務器,但現在使用sql server 是一個問題? – cks

相關問題