Welcome Guest, Not a member yet? Register   Sign In
Session undefined property in MY_Controller
#1

[eluser]Unknown[/eluser]
Hi,

I want to extend the controller with a login auth. I have to call a session for that but it crashes:

Message: Undefined property: MY_Controller::$session

Filename: libraries/MY_Controller.php

Line Number: 22



Code:
<?php if (! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Controller extends Controller
{
    function MY_Controller()
    {
        parent::Controller();
        $this->auth();
    }
    
    function auth()
    {

        //build cookie for current page
        $cookie = array(
           'name'   => 'cur_page',
           'value'  => $this->uri->uri_string(),
           'expire' => '86500'
        );
                
        //logged in on frontend?
        if ($this->session->userdata('user_id'))
        {
            //get user from model
            $getuser = $this->dms_general->get_user_by($userid);
            
            //refine user info
            foreach($result as $getuser)
            {
                $sdata['user_group'] = $result->users_group_id;
            }
            
            //put usergroup in session
            $this->session->set_userdata($sdata);
            
            //get current page from cookie if exists
            $cur_page = get_cookie('cur_page');
            if($cur_page){
                
                //go to that page
                redirect('/'.$cur_page);
                
            }else{
                //else go home
                redirect('/home');
            }
            
        } else {
            //not logged in? delete cookie and set new cookie
            delete_cookie('cur_page');
            set_cookie($cookie);
            //back to frontend
            redirect('../home');
        }      
    }
}

What am I doing wrong? The session class is being loaded in the autoload. I see a lot of examples online where it's no problem to extend the controller calling sessions in it, so I'm a bit confused.

Thanks,
L.
#2

[eluser]imn.codeartist[/eluser]
can you post your class where you extend your MY_Controller... what I am seeing is you haven't set the value in your session variable
Code:
$this->session->userdata('scheme_id')

if CI don't find the variable it ouputs error.

if your controller checks session value before
Code:
$this->session->set_userdata('scheme_id','some value')

it outputs error.
#3

[eluser]Unknown[/eluser]
Thanks dixcoder. But I think that doesn't make any sense. If I set the userdata right before I check it, unfortunately it still errors on that undefined property... You need to use get_instance() to call the CI superclass as stated in the docs. But that doesn't solve the prob either.




Theme © iAndrew 2016 - Forum software by © MyBB