公共__Construct会话错误Codeigniter

时间:2014-06-24 06:16:52

标签: php codeigniter

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

使用if语句无法在公共构建

中显示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;
      }

}

1 个答案:

答案 0 :(得分:0)

发现了下面的尝试,现在正在工作。

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