duanjiao6730 2017-06-17 10:25
浏览 101
已采纳

我有一个登录问题用户名和密码是正确的

here i am trying to log in with username "admin" and password "admin" with form validation and putting those value in session but while checking in database it is not returning any value???? my controller which verifies username and password is

 <?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
?>

i am using sql server 2008 UserName = admin Password =admin USERTYPEID=1

  • 写回答

2条回答 默认 最新

  • duanjie2701 2017-06-17 14:38
    关注

    You should try this, I have improved some code and tested on my local server. it's working fine. Hope it will solve your problem. Here I assuming that your model works fine.

        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();
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 手机连接电脑热点显示无ip分配
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大