简体   繁体   English

在Codeigniter中重定向后未设置会话

[英]Session is not set after redirect in codeigniter

I've created a project in Codeigniter. 我已经在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. 我的问题是登录时,身份验证控制器显示了在会话$ this-> session-> userdata(“ logged_in”)中设置的值,但未重定向到仪表板。

I also changed the PHP version on the live server from PHP 7.1 to PHP 5.6 but it's still not working. 我还将实时服务器上的PHP版本从PHP 7.1更改为PHP 5.6但仍无法正常工作。 Session works perfectly on local server with xampp but not working on live server 会话在具有xampp的本地服务器上完美运行,但在实时服务器上不起作用

Auth_model 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(); 更改$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/'; $config['sess_save_path'] = FCPATH . 'application/cache/sessions/'; in config.php 在config.php中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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