2017-06-17 94 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

相关问题