简体   繁体   中英

Session is not set after redirect in codeigniter

I've created a project in Codeigniter. My problem is when I log in, auth controller shows the value that is set in session $this->session->userdata("logged_in") but it is not redirecting to dashboard.

I also changed the PHP version on the live server from PHP 7.1 to PHP 5.6 but it's still not working. Session works perfectly on local server with xampp but not working on live server

Auth_model

  public function Authentification() {
    $notif = array();
    $email = $this->input->post('email',TRUE);
    $password = Utils::hash('sha1', $this->input->post('password'), AUTH_SALT);

    $this->db->select('*');
    $this->db->from('users');
    $this->db->where('email', $email);
    $this->db->where('password', $password);
    $this->db->limit(1);

    $query = $this->db->get();

    if ($query->num_rows() == 1) {
        $row = $query->row();
        if ($row->is_active != 1) {
            $notif['message'] = 'Your account is disabled !';
            $notif['type'] = 'warning';
        } else {
            $sess_data = array(
                'users_id' => $row->users_id,
                'first_name' => $row->first_name,
                'email' => $row->email
            );
           $this->session->set_userdata('logged_in', $sess_data);

        }
    } else {
        $notif['message'] = 'Username or password incorrect !';
        $notif['type'] = 'danger';
    }

    return $notif;
}

Auth controller

 class Auth extends CI_Controller {

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

    Utils::no_cache();
    if ($this->session->userdata('logged_in')) {
        redirect(base_url('dashboard'));
        exit;
    }
}



public function index() {
        redirect(base_url('home'));
    }

 public function login() {
    $data['title'] = 'Login';
    $this->load->model('auth_model');

    if (count($_POST)) {
        $this->load->helper('security');
        $this->form_validation->set_rules('email', 'Email address', 'trim|required|valid_email|xss_clean');
        $this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean');

        if ($this->form_validation->run() == false) {
            // $data['notif']['message'] = validation_errors();
            // $data['notif']['type'] = 'danger';
              $status = validation_errors();
             if ( $this->input->is_ajax_request() ) {
                echo json_encode($status);
                exit;
            }
        } 
        else {
            $data['notif'] = $this->auth_model->Authentification();
                  //  it show the result here but not redirect to dashboard
             //    print_r($this->session->userdata("logged_in"));
        //         die("auth/login");
        }
    }


    if ($this->session->userdata('logged_in')) {
        redirect(base_url('dashboard'));
        exit;
    }

    /*
     * Load view
     */
    $this->load->view('includes/header', $data);
    $this->load->view('home/index');
    $this->load->view('includes/footer');
}

dashboard

  class Dashboard extends CI_Controller {

var $session_user;

function __construct() {
    parent::__construct();
      $this->load->model('auth_model');
     $this->load->helper('tool_helper');

    Utils::no_cache();
    if (!$this->session->userdata('logged_in')) {
        redirect(base_url('home'));
        exit;
    }
    $this->session_user = $this->session->userdata('logged_in');

}

/*
 * 
 */

public function index() {
    $data['title'] = 'Dashboard';
    $data['session_user'] = $this->session_user;
    // print_r($this->session->userdata("logged_in")); //its show empty
    $data['items'] = $this->auth_model->get_all_products();

    $this->load->view('includes/header', $data);
    // $this->load->view('includes/navbar');
    $this->load->view('includes/navbar_new');
    $this->load->view('dashboard/index');
    $this->load->view('includes/footer');
}

I don't know why session not set. I have been stuck in this for a week. Please help me out.

Try this.

Change $config['sess_save_path'] = sys_get_temp_dir(); to $config['sess_save_path'] = FCPATH . 'application/cache/sessions/'; $config['sess_save_path'] = FCPATH . 'application/cache/sessions/'; in config.php

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