0
votes

I'm using php codeigniter for my project. In my login page if username and password is invalid just load the login page, else load the home. if invalid, First time it loads the login page again given the wrong details for login one controller name is added in url like local turns like localhost/project name/administrator/administrator/login_authentication

my code is

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');
    }
}

Admin authentication function

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();
}

I'm trying more than one time given not correct information for login everytime one controller name added in url. Please help me.

Thanks in advance.

2
Please paste here your controller code. - Naveed Ramzan
Instead of trying to reinvent the wheel, why not use something that is already available to give you a head start. Look into [Bonfire][1], it already has User registration, authentication, and Role-Base Access Control so you can focus on real functionality of your application. [1]: cibonfire.com - Shairyar
Can you post your admin_authentication() function? - Craig
admin_authentication funciton is updated - user3265980

2 Answers

0
votes

change

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

to

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

and change

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

in all area to

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

and try....

0
votes

first of all check if there is an post request in your function login_authentication() like this:

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

Here is your function:

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');
    }
}

In your function 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');
    }
}