未定义的属性:索引::$session



如何解决问题 当我运行此代码时,我收到以下错误 我已经加载了会话库,但仍然收到此错误
这是我的控制器

public function __construct(){
    parent::__construct();        
}
public function index() {
    $this->load->database();
    $this->load->helper('form', 'url');
    $this->load->library('session');
}
public function process() {     
    $this->load->model('login_model');
    $result = $this->login_model->validate();
    if (!$result) {
        $msg = '<font color=red>Invalid username and/or password.</font><br />';
        $this->load->view(base_url().'index',$msg);
    } else {
        if ($_SESSION['utype'] == 2 || $_SESSION['utype'] == 1) {
            redirect(base_url() . 'admin/dashboard');
        }
        if ($_SESSION['utype'] == 3 && !empty($_POST['urlValue'])) {
            redirect($_POST['urlValue']);
        }
        if (empty($_POST['urlValue'])) {
            redirect('/');
        }
    }
}

这是我的模型

   public function validate() { 
    $username = $this->security->xss_clean($this->input->post('email'));
    $password = $this->security->xss_clean($this->input->post('password'));            
    $pwd = base64_encode($password); 
    $this->db->where('email', $username);
    $this->db->where('password', $pwd);      
    $query = $this->db->get('adduser');
    $row = $query->row();
    if (count($row) > 0) {           
        $row = $query->row();
        $data = array(
            'uid' => $row->id,
            'uname' => $row->firstname,
            'utype' => $row->usertype,
            'uemail' => $row->email,
            'validated' => true
        );
        $this->session->set_userdata($data);
        return $row;
    }      
    return $row;
}

我遇到这个问题:

 Severity: Notice
 Message: Undefined property: Index::$session
 Filename: core/Model.php
 Line Number: 77
 Backtrace:
 File: D:xampphtdocssavepaiseapplicationmodelsLogin_model.php
 Line: 42
 Function: __get
 File: D:xampphtdocssavepaiseapplicationcontrollersIndex.php
 Line: 31
 Function: validate
 File: D:xampphtdocssavepaiseindex.php
 Line: 315
 Function: require_once
 Fatal error: Call to a member function set_userdata() on a non-object in 
 D:xampphtdocssavepaiseapplicationmodelsLogin_model.php on line 42
   A PHP Error was encountered
 Severity: Error
 Message: Call to a member function set_userdata() on a non-object
 Filename: models/Login_model.php
 Line Number: 42

我认为您不仅需要在controller constructor函数中加载all library index function

public function __construct(){
    parent::__construct();
    $this->load->database();
    $this->load->helper('form', 'url');
    $this->load->library('session');        
}

更新1:你可以像这样使用自动加载

转到applications/config/autoload.php,您可以在其中编辑所需的内容。

它们在数组中,并按包、库、帮助程序、配置、语言和模型分隔。

$autoload['libraries'] = array('database', 'session');
$autoload['helper'] = array('url', 'html', 'form');

最新更新