首页 文章

登录部分在php codeigniter中运行不正常

提问于
浏览
0

我正在为我的项目使用php codeigniter . 在我的登录页面中,如果用户名和密码无效,只需加载登录页面,否则加载主页 . 如果无效,首次加载登录页面,如果登录错误的详细信息,则在网址中添加一个控制器名称,如localhost / project name / administrator / administrator / login_authentication

我的代码是

function index()
{
  if($this->session->userdata('usertype') != '')
    {
        redirect('administrator/administrator_view');
    }
    else
    {   
       $this->load->view('login');
     }  
}
    function login_authentication()
{

    $username=$this->input->post('username');
    $password=$this->input->post('password');
    $user = $this->administrator->admin_authentication($username,$password);
    if(count($user) == 1)
    {

        foreach($user as $admin_value)
        {
            $user_name=$admin_value['UserName'];
            $usertype=$admin_value['UserType'];
        }
        $session_data = array(
               'username'  => $user_name,
               'usertype'  => $usertype,
        );
        $this->session->set_userdata($session_data);
        if($usertype == 1)
        {
            redirect('administrator/administrator_view');
        }
    }
    else
    {
        $data['Invalid_Login']="Invalid Username and Password";
        $this->load->view('login',$data);
    }

}
function administrator_view()
{
   if($this->session->userdata('usertype') == '')
    {
        redirect('administrator');
    }
    else
    {   
     $data['heading'] = '';
    $this->load->view('header', $data);
    $this->load->view('dashboard', $data);
    $this->load->view('footer');
    }
}

管理员认证功能

function admin_authentication($username, $password)
{
    $this->db->select('*');
    $this->db->from('user');
    $this->db->where('UserName',$username);
    $this->db->where('Password',$password);
    $query = $this->db->get();
    return $query->result_array();
}

每次在url中添加一个控制器名称时,我会尝试多次登录时没有正确的信息 . 请帮我 .

提前致谢 .

2 回答

  • 0

    更改

    $this->session->set_userdata($session_data);
    

    $this->session->set_userdata(('some_name', $session_data);
    

    并改变

    if($this->session->userdata('usertype') == '')
    

    在所有地区

    $ses = $this->session->userdata('some_name');
    if($ses['usertype'] == '')
    

    并尝试....

  • 0

    首先检查 function login_authentication() 中是否有发布请求,如下所示:

    function login_authentication()
    {
        if( $this->input->post(null) ){
            //your authentication code here
        }else{
            //load the login view here
        }
    }
    

    这是你的功能:

    function login_authentication(){
        if( $this->input->post(null) ){ //check if there is an post request
            $username=$this->input->post('username');
            $password=$this->input->post('password');
            $user = $this->administrator->admin_authentication($username,$password);
            print_r( $user );die(); //the user array as returned from the model see if its correct or not
            if(count($user) == 1)
            {
    
                foreach($user as $admin_value)
                {
                    $user_name=$admin_value['UserName'];
                    $usertype=$admin_value['UserType'];
                }
                $session_data = array(
                       'username'  => $user_name,
                       'usertype'  => $usertype,
                );
                print_r( $session_data );die; //see if it builds the correct array or not
                //$this->session->set_userdata($session_data);
                $this->session->set_userdata('user_info',$session_data);    //to read the username use like $this->session->userdata['user_info']['username'];
                if($usertype == 1)
                {
                    redirect('administrator/administrator_view');
                }
            }else{                      //invalid credentials load the login view
                $this->session->set_flashdata('Invalid_Login', 'Invalid username or password!'); //to echo in view use $this->session->flashdata('Invalid_Login');
                redirect('administrator', 'refresh');
            }
        }else{                          //redirect to index function now
            redirect('administrator', 'refresh');
        }
    }
    

    在你的函数 administrator_view() 中,

    function administrator_view(){
        if( !$this->session->userdata('user_info') ){
            print_r( $this->session->all_userdata() );die('no session set redirecting'); //the session is not set here
            redirect('administrator');
        }
        else{   
            $data['heading'] = '';
            $this->load->view('header', $data);
            $this->load->view('dashboard', $data);
            $this->load->view('footer');
        }
    }
    

相关问题