CodeIgniter 3.0.3 ci_session未将会话数据保存到ci_sesstion表中 [英] CodeIgniter 3.0.3 ci_session not saving session data to ci_sesstion table

查看:222
本文介绍了CodeIgniter 3.0.3 ci_session未将会话数据保存到ci_sesstion表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm new to CodeIgniter and I'm having some issues with the login \ logout ci_session data now saving at all. I have setup the standard ci_session table using the script in the CodeIgniter docs and have setup the config file to allow session to be stored in the relevant table, but nothing is.

会话的config.php settinh:

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文件的登录表单:

Here are the login form MVC files:

模型登录

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

控制器登录

<?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表时,它为空。

The login seems to work and redirects to the members area however when I check the ci_session table it's empty.

在成员区域中,我想注销一次,这应该破坏当前会话,然后重定向到不起作用的index.php主页。

Once in the members area I want to logout which should destroy the current session and then redirect to the index.php home page which is doesn't do.

这里是成员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();
        }
    }
}

查看成员区域

VIEW MEMBER AREA

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

大家的任何建议或指导都是巨大的帮助。

Any advice or direction from you folks would be a huge help.

谢谢!

推荐答案

在config.php文件中,您需要启用ci会话:

In your config.php file you need to enable ci session:

$config['sess_use_database'] = TRUE;

来自CI用户指南:

一旦启用,Session类将在数据库中存储会话数据。

Once enabled, the Session class will store session data in the DB.

确保已在配置文件中将表名指定为好:

Make sure you've specified the table name in your config file as well:

$config['sess_table_name'] = 'ci_sessions';

您可以根据表名更改此名称。

You can change this name as per your table name.

这篇关于CodeIgniter 3.0.3 ci_session未将会话数据保存到ci_sesstion表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆