duanhemou9834
duanhemou9834
2016-01-05 22:24
浏览 97
已采纳

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

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

Here are the login form MVC files:

MODEL LOGIN

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

VIEW LOGIN

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

}

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

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.

Here is the members V\C

CONTROLLER MEMBER AREA

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

This is the logout V and C

CONTROLLER LOGOUT

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

VIEW LOGOUT

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

Thanks!

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • duanlijia5864
    duanlijia5864 2016-01-06 03:29
    已采纳

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

    $config['sess_use_database'] = TRUE;
    

    From the CI User Guide:

    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.

    点赞 评论
  • duanpen9294
    duanpen9294 2016-01-06 03:40

    Change your config.php as below, it stores the sessions in database.

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

    and in CI 3.x the db table structure is changed, in database you create a table that you mentioned in config file (here its 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`)
    );
    

    Refer the link CI 3.0.3 Session Library.

    Hope this helps you.

    点赞 评论

相关推荐