Jani
Jani

Reputation: 19

Undefined property: Index::$session

How Can I Solve the issue When I am running this code I am getting the following error I have already loaded session library still i am getting this error
This is my controller

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

This is my model

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

I am getting this problem:

 Severity: Notice
 Message: Undefined property: Index::$session
 Filename: core/Model.php
 Line Number: 77
 Backtrace:
 File: D:\xampp\htdocs\savepaise\application\models\Login_model.php
 Line: 42
 Function: __get
 File: D:\xampp\htdocs\savepaise\application\controllers\Index.php
 Line: 31
 Function: validate
 File: D:\xampp\htdocs\savepaise\index.php
 Line: 315
 Function: require_once
 Fatal error: Call to a member function set_userdata() on a non-object in 
 D:\xampp\htdocs\savepaise\application\models\Login_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

Upvotes: 0

Views: 1092

Answers (1)

JYoThI
JYoThI

Reputation: 12085

I think you need to load all library inside the controller constructor function not only in index function

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

Update 1 : you can use autoload like this

Go to applications/config/autoload.php and in there you can edit what you need.

They are in arrays and seperated by packages, libraries, helpers, config, languages and models.

Example

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

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

Upvotes: 1

Related Questions