CodeIgniter 3.0.3 ci_session 未将会话数据保存到 ci_sesstion 表

0 php codeigniter-3

我是 CodeIgniter 的新手,现在保存的登录\注销 ci_session 数据存在一些问题。我已经使用 CodeIgniter 文档中的脚本设置了标准 ci_session 表,并设置了配置文件以允许会话存储在相关表中,但什么也没有。

config.php 会话设置:

$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;
Run Code Online (Sandbox Code Playgroud)

以下是登录表单 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();
     }   
}
Run Code Online (Sandbox Code Playgroud)

查看登录

<?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>
Run Code Online (Sandbox Code Playgroud)

控制器登录

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

}
Run Code Online (Sandbox Code Playgroud)

登录似乎可以正常工作并重定向到会员区域,但是当我检查 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();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

查看会员区

<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>
Run Code Online (Sandbox Code Playgroud)

这是注销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');
    }
}
Run Code Online (Sandbox Code Playgroud)

查看 退出

<?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>
Run Code Online (Sandbox Code Playgroud)

你们的任何建议或指导都会有很大的帮助。

谢谢!

小智 5

如下更改 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;
Run Code Online (Sandbox Code Playgroud)

在 CI 3.x 中,数据库表结构发生了变化,在数据库中您创建了一个在配置文件中提到的表(这里是 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`)
);
Run Code Online (Sandbox Code Playgroud)

请参阅链接CI 3.0.3 Session Library

希望这对您有帮助。