2016-10-07 45 views
0

我是codeigniter的初學者。從Here學習代碼。現在我需要某人幫助繼續session爲什麼我得到這個Codeigniter會話錯誤

//config.php

<pre> <code>  
    $config['sess_driver'] = 'database';  
    $config['sess_save_path'] = 'ci_sessions';  
    $config['sess_cookie_name'] = 'cisessions';  
    $config['sess_expiration'] = 7200;  
    $config['sess_match_ip'] = FALSE;  
    $config['sess_time_to_update'] = 300;  
    $config['sess_regenerate_destroy'] = FALSE;  
</code> </pre> 

//數據庫表


    CREATE TABLE IF NOT EXISTS `ci_sessions` (
     `session_id` varchar(80) NOT NULL, 
     `ip_address` varchar(45) NOT NULL, 
     `user_agent` varchar(120) NOT NULL, 
     `last_activity` int(10) unsigned DEFAULT 0 NOT NULL, 
     `user_data` text, 
     KEY `last_activity` (`last_activity`) 
    ); 

mode_user



    class Model_user extends MY_Model{ 
    protected $_table_name = 'user'; 
    protected $_order_by = 'name'; 
    public $rules = array(
     'email' => array('field' => 'email', 'label' => 'Email', 'rules' => 'trim|required|valid_email|xss_clean'), 
     'password' => array('field' => 'password', 'label' => 'Password', 'rules' => 'trim|required'), 
    ); 

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

    public function login(){ 
     $user = $this->get_by(array(
      'email'  => $this->input->post('email'), 
      'password' => $this->hash($this->input->post('password')), 
     ), TRUE); 
     if(count($user)){ 
      $data = array(
       'name'  => $user->name, 
       'email'  => $user->email, 
       'id'  => $user->id, 
       'loggedin' => TRUE, 
      ); 
      $this->session->set_userdata($data); 
     } 
    } 

    public function logout(){ 
     $this->session->sess_destroy(); 
    } 

    public function loggedin(){ 
     return (bool) $this->session->userdata('loggedin'); 
    } 

    public function hash($string){ 
     return hash('sha512'. $string . config_item('encryption_key')); 
    } 
    } 

用戶控制器



    class User extends Backend_Controller{ 
    function __consrtuct(){ 
     parent::__consrtuct(); 
    } 

    public function login(){ 
     $rules = $this->model_user->rules; 
     $this->form_validation->set_rules($rules); 
     if($this->form_validation->run() == TRUE){ 
      //login & redirect 
      $this->model_user->login(); 
     } 
     $this->data['subview'] = 'admin/user/login'; 
     $this->load->view('admin/_layout_modal', $this->data); 
    } 
    } 

得到錯誤這樣

A PHP Error was encountered 
Severity: Warning 
Message: Unknown: Failed to write session data (user). Please verify that the current setting of session.save_path is correct (ci_sessions) 
Filename: Unknown 
Line Number: 0 
Backtrace: 

請指導我解決這個錯誤

感謝

+0

將會話保存到數據庫的任何理由? –

+0

@AbdullaNilam:很久以後你怎麼樣? – devpro

+1

@devpro精細。更換名字。多數民衆贊成你不會注意到我 –

回答

0

的笨3.1.0文件說,爲創建會話表:

CREATE TABLE IF NOT EXISTS `ci_sessions` (
    `id` varchar(40) NOT NULL, 
    `ip_address` varchar(45) NOT NULL, 
    `timestamp` int(10) unsigned DEFAULT 0 NOT NULL, 
    `data` blob NOT NULL, 
    KEY `ci_sessions_timestamp` (`timestamp`) 
); 

// When sess_match_ip = TRUE 
ALTER TABLE ci_sessions ADD PRIMARY KEY (id, ip_address); 

// When sess_match_ip = FALSE 
ALTER TABLE ci_sessions ADD PRIMARY KEY (id); 

也在數據庫中存儲會話時必須提到一些限制:
- 只有你的def可以使用數據庫連接(或者從控制器以$ this-> db訪問的連接)。
- 您必須啓用查詢生成器。
- 您不能使用持續連接。
- 您無法使用啓用了cache_on設置的連接。

更多信息可以在這裏找到:http://www.codeigniter.com/user_guide/libraries/sessions.html#session-drivers

也許你的數據庫連接配置不正確或不匹配的會話配置?

+0

不存儲時,我改變 'db_debug'=> FALSE。到'db_debug'=>(ENVIRONMENT!== '生產') getiing此錯誤 未知列在 '字段列表' '數據' SELECT'data' FROM'ci_sessions' WHERE'id' = '59538fa0a69463b18761e21f07a8c77df968f980' 文件名:C:/程序文件(x86)/Ampps/www/arc/system/database/DB_driver.php 行號:691 –

+0

然後它似乎不使用Codeigniter 3.x - 您使用的是2.x:https:// www.codeigniter.com/userguide2/libraries/sessions.html – zuluk