2017-08-08 95 views
0

我在Codeigniter 3.1.4中有一個項目,並且突然沒有在其他頁面上加載會話變量。 I 配置爲自動載入autoload.php中的庫,但沒有起作用,我將CI升級到3.1.5,但也沒有工作。Codeiniter不會將會話加載到其他頁面

我用本機會話太但沒有成功。

我開始了一個只有一個控制器的測試項目,但沒有奏效。

測試控制器的代碼如下:

<?php 

defined('BASEPATH') OR exit('No direct script access allowed'); 

class Home extends CI_Controller { 
    public function __construct() { 
     parent::__construct(); 
    } 

    public function index() { 
     $this->session->set_userdata('test', 'test1'); 

     echo $_SESSION['test'] . '<br>'; 
     echo anchor('home/test', 'Validate'); 
    } 

    public function test() { 
     echo '<pre>'; 
     print_r($_SESSION); 
    } 
} 

這是我對會話和Cookie的config.php文件設置:

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

$config['cookie_prefix'] = ''; 
$config['cookie_domain'] = ''; 
$config['cookie_path']  = '/'; 
$config['cookie_secure'] = FALSE; 
$config['cookie_httponly'] = FALSE; 

我試過會話驅動程序數據庫,但仍然是同樣的問題。

+0

將您的會話和cookie設置從** application/config/config.php **添加到問題中。 – DFriend

+0

朋友,代碼已被編輯! –

+0

您需要設置會話路徑。把'$ config ['sess_save_path'] = sys_get_temp_dir();'。 – Tpojka

回答

2

裝入會話庫控制器自動加載,如果不工作:

class Home extends CI_Controller { 
    public function __construct(){ 
     parent::__construct(); 
     $this->library->load('session'); 
    } 

    public function index() { 
     $this->session->set_userdata('test', 'test1'); 
    } 

    public function test() { 
     echo '<pre>'; 
     print_r($this->session->all_userdata()); //this will print the whole session array 
     print_r($this->session->userdata('test'));//this will print only session test variable 
    } 
} 
+0

沒有工作.... –

+0

什麼是錯誤或輸出? –

+0

不返回錯誤,只有一個空數組。 –

0

裝入的codeignitor會議庫的application/config/autoload.php文件

autoload.php文件

$autoload['libraries'] = array('session'); 

home.php文件 [控制器]

class Home extends CI_Controller { 

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

    public function index() { 
     $this->session->set_userdata('test', 'test1'); 
    } 

    public function test() { 
     echo '<pre>'; 
     print_r($this->session->userdata('test')); 
    } 

} 

對於更詳細的文檔遵循這個link

+0

我已經這樣做了,它在描述問題。 –

0
Load the the codeignitor session library to application/config/autoload.php file 

    $autoload['libraries'] = array('session'); 

    public function login() 
     { 
      $email=base64_encode(ucfirst(trim($this->input->post('loginid')))); 
      $pass=md5(trim($this->input->post('pass')));  
      $result=$this->db->query("SELECT * from table name where email='$email' and password='$pass); 

      if($result -> num_rows() == 1) 
      { 
       $result = $result->result(); 
       $emp_id = $result[0]->emp_id; //fetching value from the table 
       $first_name = $result[0]->first_name; //fetching value from the table 
       $roleid=$result[0]->roleid; //fetching value from the table 
       $email=$result[0]->email; //fetching value from the table 
       $admin_data = array(
           'admin_id' => $emp_id, //assigning value 
           'admin_name' => $first_name, //assigning value 
           'admin_type'=>$roleid, //assigning value 
           'admin_email'=>$email //assigning value 
           ); 
       $this->session->set_userdata($admin_data); // Setting session value 
        var_dump($admin_data); 
         or 
        print_r($admin_data) 

     } 
0

當使用$config['sess_driver'] = 'files'然後$config['sess_save_path']必須被設置爲絕對路徑會議文件的文件夾。會話文件驅動程序文檔HERE

您可能需要製作自己的目錄併爲其設置權限。您應該在Web服務器的「公共」文件夾之外創建它。在WAMP安裝中,公用文件夾的路徑可能類似於C:\wamp\www\htdocs\,Codeigniter的index.php位於該文件夾中。

在基於Linux的Apache系統上,公用文件夾的路徑可能爲/var/www/htdocs/

您需要創建與htdocs相同級別的會話文件夾。換句話說,/var/www/sessions/C:\wamp\www\sessions\。權限必須設置恰當!

CodeIgniter定義了一個常量 - FCPATH - 這是主要index.php文件的路徑。您可以使用它來構建會話文件夾的絕對路徑。

$config['sess_save_path'] = substr(FCPATH, 0, strpos(FCPATH, 'htdocs/'))."sessions/"); 

上面假定htdocs是其中index.php位於。調整上述代碼以匹配您的文件夾結構。假設FCPATH爲「/ var/www/htdocs /」,則substr(...)將返回「/ var/www/sessions /」