我是codeigniter的初学者。从Here学习codeigniter。现在我需要有人帮助继续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>
//数据库表
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)
codeigniter 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);
在数据库中存储会话时,您还必须提到一些限制:
- 只能使用您的默认数据库连接(或您从控制器访问$ this-&gt; db的连接)。
- 您必须启用查询生成器
- 您不能使用持久连接
- 您无法使用启用了cache_on设置的连接。
可在此处找到更多信息:http://www.codeigniter.com/user_guide/libraries/sessions.html#session-drivers
可能您的数据库连接未正确配置或与会话配置不匹配?