简体   繁体   中英

Why i am getting this Codeigniter session error

I am a beginner in codeigniter. Learning codeigniter from Here . Now i need someones help to continue session .

//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>

//Database table


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

user controller



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

Getting Error like this

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:

Please guide me to solve this error

Thanks

The codeigniter 3.1.0 documentation says for creating session table:

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

You also have to mention some restrictions when storing session in database:
- Only your default database connection (or the one that you access as $this->db from your controllers) can be used.
- You must have the Query Builder enabled.
- You can NOT use a persistent connection.
- You can NOT use a connection with the cache_on setting enabled.

Further Information can be found here: http://www.codeigniter.com/user_guide/libraries/sessions.html#session-drivers

Maybe your database Connection is not correctly configured or does not match session configuration?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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