duanque2413 2017-02-14 09:20
浏览 46

CodeIgniter中的会话 - 没有相应的工作

I'm working on session in codeigniter now and I'm not sure where it goes wrong. I only manage to read session for the 1st page. When I browse to other page, the session gone. The following are the var_dump for dashboard.

array(2) {
["__ci_last_regenerate"]=>
int(1487063353)
["logged_in"]=>
array(2) {
["userName"]=>
string(5) "julie"
["userRole"]=>
string(1) "1"
}}

and the following are var_dump for the second page and when i go back to dashboard

array(1) {
["__ci_last_regenerate"]=>
int(1487063602)
}

The following are my codes. Appreciate if you can help me point out the problem and solutions. Thanks in advance.

config/config.php

$config['sess_driver'] = 'files';
$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 1800; // 30 minutes
$config['sess_save_path'] = BASEPATH .'/sessions/';
$config['sess_match_ip'] = FALSE;
$config['sess_time_to_update'] = 300;
$config['sess_regenerate_destroy'] = FALSE;

libraries/SimpleLoginSecure.php

function login($user_name,$user_pass)
{
   $this->CI =& get_instance();

   // verification code goes here

   //set session data
   $user_data = $query->row_array();
   $dataSession['userName'] = $user_data['userName'];
   $dataSession['userRole'] = $user_data['userRole'];
   $this->CI->session->set_userdata('logged_in',$dataSession);
   return TRUE;
}

controller/Login.php

function __construct() {
    parent::__construct();
}

public function index() {
    $this->load->helper('form');
    $this->load->library('form_validation');

    $this->form_validation->set_error_delimiters('<p class="text-red">', '</p>');       
    $this->form_validation->set_rules('userName', 'Username', 'trim|required|xss_clean');
    $this->form_validation->set_rules('userPass', 'Password', 'trim|required|xss_clean|callback_check_database');

    if ($this->form_validation->run() == false) {
        $this->load->view('index');
    }       
}

function verify_user()
{
    $this->load->library('form_validation');
    $this->load->library('SimpleLoginSecure');

    $hasher = new PasswordHash(8, false);
    $userpass = $this->input->post('inputPassword');
    $username = $this->input->post('inputName');

    $results = $this->simpleloginsecure->login($username,$userpass);
    if($results != FALSE) {
      $SESS_data = $this->session->userdata('logged_in');
      $data['SESS_username'] = $SESS_data['userName'];
      $data['SESS_userrole'] = $SESS_data['userRole'];

      var_dump($this->session->all_userdata());
      $this->load->view('dashboard', $data);
  }
  else {
     $attempt = $this->simpleloginsecure->updatePassAttempt($userName);         
     if ($attempt != FALSE) {
        echo '<script>alert("Invalid Username or password"); window.history.back();</script>';
     }      
   }      
}

function logout() {
    $this->load->library('SimpleLoginSecure');

    // Logout
    $this->simpleloginsecure->logout();
    echo '<script>alert("Successfully Logout"); </script>';
    redirect(base_url());
}
  • 写回答

1条回答 默认 最新

  • drgweamoi473182981 2017-02-14 10:44
    关注

    Make sure you are actually loading the session library, the safest bet if its something that's going to be used 95-100% of the time stick it in autoload.

    application/config/autoload.php in the libraries array add 'session', this will ensure on initailisation it will always be loaded. Its around line 60:

    $autoload['libraries'] = array('session');
    

    The most common issue for this kind of thing occurring is not having the library loaded when you need it.

    I would also suggest using a full controller and model for account management instead of libraries or helpers, makes life much easier for you as its within the standard CI scope.

    评论

报告相同问题?

悬赏问题

  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程