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

时间:2014-06-16 08:56:46

标签: php codeigniter

我在我的项目中使用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 :(得分: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');
    }
}

答案 1 :(得分: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'] == '')

并尝试....