简体   繁体   中英

Codeigniter Login session Unknown Error

Good Day Fellows .

I have a problem in my CMS login , When i Click the login button, The login page refreshes and comes again. Session library is defined. Session encryption key is set.

Login Controller Code is :

<?php
class User extends Admin_Controller {

public function __construct(){
    parent::__construct();
}

public function login(){

    $dashboard = 'admin/dashboard';
    $this->user_m->loggedin() == FALSE || redirect($dashboard);

    $rules = $this->user_m->rules;
    $this->form_validation->set_rules($rules);
    if ($this->form_validation->run() == TRUE) {
        // We can login and redirect
        if ($this->user_m->login() == TRUE) {
            redirect($dashboard);
        }
        else {
            $this->session->set_flashdata('error', 'That email/password combination does not       exist');
            redirect('admin/user/login', 'refresh');

    }
}
$this->data['subview'] = 'admin/user/login';
$this->load->view('admin/_layout_modal', $this->data);
}

public function logout(){
    $this->user_m->logout();
    redirect('admin/user/login');
}
}

Login Model code is :

<?php
class User_M extends MY_Model
{

protected $_table_name = 'users';
protected $_order_by = 'name';
public $rules = array(
    'email' => array(
        'field' => 'email', 
        'label' => 'Email', 
        'rules' => 'trim|required|valid_email|xss_clean'
    ), 
    'password' => array(
        'field' => 'password', 
        'label' => 'Password', 
        'rules' => 'trim|required'
    )
);

function __construct ()
{
    parent::__construct();
}

public function login ()
{
    $user = $this->get_by(array(
        'email' => $this->input->post('email'),
        'password' => $this->hash($this->input->post('password')),
    ), TRUE);

    if (count($user)) {
        // Log in user
        $data = array(
            'name' => $user->name,
            'email' => $user->email,
            'id' => $user->id,
            'loggedin' => TRUE,
        );
        $this->session->set_userdata($data);
    }
}

public function logout ()
{
    $this->session->sess_destroy();
}

public function loggedin ()
{
    return (bool) $this->session->userdata('loggedin');
}

public function hash ($string)
{
    return hash('sha512', $string . config_item('encryption_key'));
}
}

I suggest better to put login view in else condition ,

public function login(){
$dashboard = 'admin/dashboard';
$this->user_m->loggedin() == FALSE || redirect($dashboard);
$rules = $this->user_m->rules;
$this->form_validation->set_rules($rules);
if($this->input->post()) {       //check if request if post
if ($this->form_validation->run() == TRUE) {
    // We can login and redirect
    if ($this->user_m->login() == TRUE) {
        redirect($dashboard);
    }
    else {
        $this->session->set_flashdata('error', 'That email/password combination does not       exist');
        redirect('admin/user/login', 'refresh');

   } 
  }
 } else {     //defult login page
  $this->data['subview'] = 'admin/user/login';
  $this->load->view('admin/_layout_modal', $this->data);
} }

If you still faces the problem , please manually debug and check where it getting stuck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM