2016-12-18 107 views
2

我在開發應用程序的CodeIgniter仍然但我意識到,當我登錄並設置會話的用戶數據,它被設置,但是重定向後消失。因此,重定向後我無法訪問會話數據。會話數據丟失後重定向笨3.1.2

這是我的登錄腳本

public function login(){ 

    if(!empty($_SESSION['user_id'])){//Meaning you are logged in 
     //We inform you 
     $this->session->set_flashdata('msg', "<div class='alert alert-success'><span class=''></span> You are already logged in as <strong>".$this->session->user_name."</strong></div>"); 

     //And send you back to your dashboard 
     return redirect('/account/dashboard'); 
    } 

    if($this->input->post()){ 
    $this->load->library('form_validation'); 
    $this->form_validation->set_rules('email','Email','required|valid_email'); 
    $this->form_validation->set_rules('password','Password','required'); 

     if($this->form_validation->run() == TRUE){ 

      $creds=['email'=>$this->input->post('email'),'password'=>$this->input->post('password')]; 
      $this->load->model('user/user_model'); 

      $user=$this->user_model->login($creds); 

      if(!$user){ 
       $data['error_msg']="Inavlid login details. Please retry"; 
      }else{ 

      $user->user_type=(!empty($user->user_type))?:'2'; 

       $userdata=['user_id'=>$user->id,'user_name'=>$user->name,'user_email'=>$user->email,'user_type'=>$user->user_type]; 

       $this->session->set_userdata($userdata); 
        $this->session->set_flashdata('msg', '<div class="alert alert-success"><span class="fa fa-check"></span> Logged in as '.$this->session->userdata('user_name').'</div>'); 

       return redirect('account/dashboard'); 
      } 

     } 
    } 

    $data['title']="Login"; 
    $this->load->view('template/auth/header'); 
    $this->load->view('account/login',$data); 
    $this->load->view('template/auth/footer'); 

} 

,這是我的config/congig.php文件

$config['base_url'] = 'http://localhost/tmpad/'; 

$config['allow_get_array'] = TRUE; 
$config['enable_query_strings'] = FALSE; 
$config['controller_trigger'] = 'c'; 
$config['function_trigger'] = 'm'; 
$config['directory_trigger'] = 'd'; 

$config['encryption_key'] = '4%^&*9799809-nkhdfioup'; 

$config['sess_driver'] = 'database'; 
$config['sess_cookie_name'] = 'ci_session'; 
$config['sess_expiration'] = 7200; 
$config['sess_save_path'] = 'ci_sessions'; 
$config['sess_match_ip'] = FALSE; 
$config['sess_time_to_update'] = 300; 
$config['sess_regenerate_destroy'] = FALSE; 

$config['cookie_prefix'] = ''; 
$config['cookie_domain'] = 'http://localhost/tmpad/'; 
$config['cookie_path']  = '/'; 
$config['cookie_secure'] = FALSE; 
$config['cookie_httponly'] = FALSE; 

$config['csrf_protection'] = TRUE; 
$config['csrf_token_name'] = 'csrf_test_name'; 
$config['csrf_cookie_name'] = 'csrf_cookie_name'; 
$config['csrf_expire'] = 7200; 
$config['csrf_regenerate'] = TRUE; 
$config['csrf_exclude_uris'] = array(); 

會議是自動加載的config/autoload.php

我會感謝任何指導或幫助。 謝謝

回答

0

如果您評論重定向行並創建另一個操作以轉儲會話(例如dumpsession),那麼如果您登錄並最終將瀏覽器指向BASEURL/controller/dumpsession,則您的代碼看起來很好會話數據?

也許問題不是重定向。

+0

謝謝@Diego。我按照你的建議完成了,但函數(在重定向之前)顯示了會話變量。但重定向後,會話變量丟失 – Josh

+0

我有同樣的問題 –

9

我不確定究竟是什麼問題。最近我遇到這個太..

這是在我的開發運行php7.0前的工作。

目前,它只是在我的生產服務器運行工作nginx的和PHP 5.6。我的開發服務器似乎不工作,並不斷在會話表中重新生成新行。我的開發服務器使用的是家庭虛擬機開發環境中的php7.1,通常用於Laravel項目。

我設法採取這一步驟來克服這一點。

1)加入//轉到系統/庫/會話/ session.php文件

2)註釋在session_start()。我們要重新定位sessionn_start()。

3)再往線315,它說安全爲王,並註釋掉,直到線351

enter image description here

4)然後轉到您的index.php(根的index.php)

5)在頂部添加session_start()一次。

enter image description here

6)好了再試一次。希望它有效。我的猜測是它不適用於php 7.1,並且需要在這個Session.php文件中完成一些更新。

我的CI版本3.1.1是

enter image description here

+0

作品對我:)謝謝 –

+0

完美,這個解決了它,非常感謝你 – Sagito

+0

非常感謝你,你救了我的一天先生 –