__Construct会话错误编码器


Public __Construct Session Error Codeigniter

在codeigniter中,当我将if语句添加到我的用户库公共构造区域时,它现在显示会话为空白数组()。

With if statement无法在公共构造中显示使用if语句的会话

Array
(
)

没有if语句

Array
(
    [session_id] => ********
    [ip_address] => ********
    [user_agent] => ********
    [last_activity] => ********
    [user_data] => 
)

当我删除公共构造区域的if语句时,会话返回。

我需要能够有我的if语句工作在我的公共建设区域。

public function __construct() {
$this->CI =& get_instance();
$this->CI->load->database();
$this->CI->load->library('session');
$this->CI->load->library('form_validation');
$this->CI->load->library('security/encryption');
if(null !== ($this->CI->session->userdata('user_id'))) {
  $username = $this->CI->input->post('username');
  $password = $this->CI->input->post('password');
  $this->CI->db->select('user_id');
  $this->CI->db->from('user');
  $this->CI->db->where('username', $username);
  $this->CI->db->where('password',  hash('sha512', $password));
  $this->CI->db->where('status', "1");
  $user_query = $this->CI->db->get();
  if($user_query->num_rows) {
    $this->user_id = $user_query->row('user_id');
    $this->username = $this->CI->input->post('username');
    $this->CI->db->query("UPDATE " . $this->CI->db->dbprefix . "user SET ip = '" . $this->CI->input->ip_address() . "' WHERE user_id = '" . (int)$this->CI->session->userdata('user_id') . "'");
   } else {
     $this->logout();
   }
  }
}
用户自由

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Users {
      public function __construct() {
            $this->CI =& get_instance();
            $this->CI->load->database();
            $this->CI->load->library('session');
            $this->CI->load->library('form_validation');
            $this->CI->load->library('security/encryption');
            if(null !== ($this->CI->session->userdata('user_id'))) {
                  $username = $this->CI->input->post('username');
                  $password = $this->CI->input->post('password');
                  $this->CI->db->select('user_id');
                  $this->CI->db->from('user');
                  $this->CI->db->where('username', $username);
                  $this->CI->db->where('password',  hash('sha512', $password));
                  $this->CI->db->where('status', "1");
                  $user_query = $this->CI->db->get();
                  if($user_query->num_rows) {
                        $this->user_id = $user_query->row('user_id');
                        $this->username = $this->CI->input->post('username');
                        $this->CI->db->query("UPDATE " . $this->CI->db->dbprefix . "user SET ip = '" . $this->CI->input->ip_address() . "' WHERE user_id = '" . (int)$this->CI->session->userdata('user_id') . "'");
                  } else {
                        $this->logout();
                  }
            }
      }
      public function login() {
            $username = $this->CI->input->post('username');
            $password = $this->CI->input->post('password');
            $this->CI->db->select('user_id');
            $this->CI->db->from('user');
            $this->CI->db->where('username', $username);
            $this->CI->db->where('password',  hash('sha512', $password));
            $this->CI->db->where('status', "1");
            $user_query = $this->CI->db->get();
            if($user_query->num_rows() == 1) {
                  $this->user_id = $user_query->row('user_id');
                  $this->username = $this->CI->input->post('username');
                  $data = array(
                        'isLogged' => $this->user_id,
                        'user_id' => $this->user_id,
                        'username' => $this->CI->input->post('username')
                  );
                  $this->CI->session->set_userdata($data);
                  return true;
            } else {
                  return false;
            }
      }
      public function logout() {
            $this->CI->session->sess_destroy();
      }
      public function isLogged() {
            return $this->user_id;
      }
     public function getId() {
            return $this->user_id;
      }
      public function getUserName() {
            return $this->username;
      }
}

我在下面试了一下,现在效果不错。

if(null !== ($this->CI->session->userdata('user_id'))) {
              $this->CI->db->select('user_id');
              $this->CI->db->from('user');
              $this->CI->db->where('status', "1");
              $user_query = $this->CI->db->get();
              if($user_query->num_rows) {
                    $this->user_id = $user_query->row('user_id');
                    $this->CI->db->query("UPDATE " . $this->CI->db->dbprefix . "user SET ip = '" . $this->CI->input->ip_address() . "' WHERE user_id = '" . (int)$this->CI->session->userdata('user_id') . "'");
              } else {
                    $this->logout();
              }
        }