【问题标题】:Session is not set after redirect in codeigniter在codeigniter中重定向后未设置会话
【发布时间】:2018-06-04 05:31:48
【问题描述】:

我在 Codeigniter 中创建了一个项目。我的问题是当我登录时,身份验证控制器显示会话 $this->session->userdata("logged_in") 中设置的值,但它没有重定向到仪表板。

我还将实时服务器上的 PHP 版本从 PHP 7.1 更改为 PHP 5.6,但仍然无法正常工作。会话在带有 xampp 的本地服务器上完美运行,但不能在实时服务器上运行

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

身份验证控制器

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

仪表板

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

我不知道为什么没有设置会话。我已经被困在这一个星期了。请帮帮我。

【问题讨论】:

  • 它在页面中显示什么而不是重定向?如果它有创建会话,则您可以看到会话 cookie。如果没有会话 cookie 表示会话创建中存在问题,则仪表板链接中存在其他问题
  • /application/config.php 文件中的会话设置是什么?您是否在日志文件中看到任何错误消息?
  • 如果会话库没有自动加载,则通过$this->load->library('session');加载它
  • 如果会话在本地而不是在实时服务器上工作,那么您需要确保 Session 和 Cookie 变量(在 config.php 中)适用于实时服务器。
  • 在 config.php 中将 $config['sess_save_path'] = sys_get_temp_dir(); 更改为 $config['sess_save_path'] = FCPATH . 'application/cache/sessions/';

标签: php codeigniter session


【解决方案1】:

试试这个。

在 config.php 中将 $config['sess_save_path'] = sys_get_temp_dir(); 更改为 $config['sess_save_path'] = FCPATH . 'application/cache/sessions/';

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-08-17
    • 1970-01-01
    • 1970-01-01
    • 2013-08-11
    • 2013-10-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多