我是CodeIgniter的新手,我现在正在保存login \ logout ci_session数据的一些问题。我已经使用CodeIgniter文档中的脚本设置了标准ci_session表,并设置了配置文件以允许会话存储在相关表中,但没有任何内容。
config.php settinh for sessions:
$config['sess_driver'] = 'files';
$config['sess_cookie_name'] = 'ci_session';
$config['sess_encrypt_cookie'] = TRUE;
$config['sess_use_database'] = TRUE;
$config['sess_expire_on_close'] = TRUE;
$config['sess_table_name'] = 'ci_session';
$config['sess_expiration'] = 60 * 30;
$config['sess_save_path'] = NULL;
$config['sess_match_ip'] = FALSE;
$config['sess_match_useragent'] = TRUE;
$config['sess_time_to_update'] = 300;
$config['sess_regenerate_destroy'] = TRUE;
以下是MVC文件的登录表单:
模型登录
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class login_model extends CI_Model
{
function __construct()
{
// Call the Model constructor
parent::__construct();
}
//get the username & password from tbl_usrs
function get_user($username, $password)
{
$this->db->where('username', $username);
$this->db->where('password', sha1($password));
$query = $this->db->get('tbl_users');
return $query->num_rows();
}
}
查看登录
<?php defined('BASEPATH') OR exit('No direct script access allowed'); ?>
<div class="container">
<div class="col-xs-12 col-sm-9 col-md-9 col-lg-9">
<div class="well col-xs-12 col-sm-12 col-md-12 col-lg-10">
<?php
$attributes = array("class" => "form-horizontal", "id" => "loginform", "name" => "loginform");
echo form_open("account/login/", $attributes);?>
<?php echo $this->session->flashdata('msg'); ?>
<fieldset>
<legend>Login</legend>
<div class="form-group">
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
<label for="txt_username" class="control-label">Username</label>
</div>
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
<input class="form-control" id="txt_username" name="txt_username" placeholder="Please enter your username here." type="text" value="<?php echo set_value('txt_username'); ?>" />
<span class="text-danger"><?php echo form_error('txt_username'); ?></span>
</div>
</div>
<div class="form-group">
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
<label for="txt_password" class="control-label">Password</label>
</div>
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
<input class="form-control" id="txt_password" name="txt_password" placeholder="Please enter your password here." type="password" value="<?php echo set_value('txt_password'); ?>" />
<span class="text-danger"><?php echo form_error('txt_password'); ?></span>
</div>
</div>
<div class="form-group">
<div class="col-xs-12 col-sm-12 col-md-6 col-lg-6">
<input id="btn_login" name="btn_login" type="submit" class="btn btn-primary btn-lg col-xs-12" style="margin-top: 5px; margin-bottom: 5px;" value="Login" />
</div>
<div class="col-xs-12 col-sm-12 col-md-6 col-lg-6">
<input id="btn_cancel" name="btn_cancel" type="reset" class="btn btn-primary btn-lg col-xs-12" style="margin-top: 5px; margin-bottom: 5px;" value="Cancel" />
</div>
</div>
</fieldset>
<?php echo form_close(); ?>
</div>
</div>
<div class="col-xs-12 col-sm-3 col-md-3 col-lg-3">
<p>test</p>
</div>
</div>
CONTROLLER LOGIN
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class login extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('account/login_model');
}
public function index()
{
$data['Title'] = "";
$data['Description'] = "";
$data['Keywords'] = "";
$data['Type'] = "";
$data['Url'] = "";
$this->load->view('includes/header', $data);
//get the posted values
$username = $this->input->post("txt_username");
$password = $this->input->post("txt_password");
//set validations
$this->form_validation->set_rules("txt_username", "Username", "trim|required|min_length[8]|max_length[30]|xss_clean");
$this->form_validation->set_rules("txt_password", "Password", "trim|required|min_length[8]|max_length[32]|xss_clean");
if ($this->form_validation->run() == FALSE)
{
//validation fails
$this->load->view('account/login');
}
else
{
//validation succeeds
if ($this->input->post('btn_login') == "Login")
{
//check if username and password is correct
$usr_result = $this->login_model->get_user($username, $password);
if ($usr_result > 0) //active user record is present
{
//set the session variables
$sessiondata = array(
'username' => $username,
'loginuser' => TRUE
);
$this->session->set_userdata($sessiondata);
redirect("members/members_area");
}
else
{
$this->session->set_flashdata('msg', '<div class="alert alert-danger text-center">Invalid username or password! Please try again.</div>');
redirect('account/login');
}
}
else
{
redirect('account/login');
}
}
$this->load->view('includes/footer');
}
}
登录似乎工作并重定向到成员区域但是当我检查ci_session表时它是空的。
一旦进入成员区域,我想注销哪个应该销毁当前会话,然后重定向到index.php主页,这是不可行的。
这是成员V \ C
控制器会员区
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Members_Area extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->loginuser();
}
public function index()
{
$data['Title'] = "";
$data['Description'] = "";
$data['Keywords'] = "";
$data['Type'] = "";
$data['Url'] = "";
$this->load->view('includes/member_header' , $data);
$this->load->view('members/members-area');
$this->load->view('includes/footer');
}
function loginuser()
{
$loginuser = $this->session->userdata('loginuser');
if (!isset($loginuser) || $loginuser != true )
{
echo 'Sorry you dont have premission to access this area. Please signup and login to gain access to this area.';
die();
}
}
}
查看会员区
<div class="container">
<div class="well col-xs-12 col-sm-12 col-md-12 col-lg-12">
<h3>Members Area</h3><br />
</div>
</div>
这是注销V和C
控制器注销
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Logout extends CI_Controller {
public function index()
{
$data['Title'] = "";
$data['Description'] = "!";
$data['Keywords'] = "";
$data['Type'] = "";
$data['Url'] = "";
$this->load->view('includes/header' , $data);
// destroy session
$this->session->sess_destroy();
// redirect to other page
redirect('account/logout');
$this->load->view('includes/footer');
}
}
查看退出
<?php defined('BASEPATH') OR exit('No direct script access allowed'); ?>
<div class="container">
<div class="col-xs-12 col-sm-9 col-md-9 col-lg-9">
<h2>You have logged out.</h2>
</div>
</div>
<div class="col-xs-12 col-sm-3 col-md-3 col-lg-3">
<p>test</p>
</div>
</div>
你们的任何建议或指示都将是一个巨大的帮助。
谢谢!
答案 0 :(得分:4)
如下所示更改config.php,它将会话存储在数据库中。
$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;
并在CI 3.x中更改了db表结构,在数据库中创建了一个在配置文件中提到的表(此处为ci_sessions)
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`)
);
请参阅链接CI 3.0.3 Session Library。
希望这会对你有所帮助。
答案 1 :(得分:1)
在config.php文件中,您需要启用ci session:
$config['sess_use_database'] = TRUE;
来自CI用户指南:
启用后,Session类会将会话数据存储在数据库中。
确保您已在配置文件中指定了表名:
$config['sess_table_name'] = 'ci_sessions';
您可以根据表名更改此名称。