• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Session Library not loading in MY_Controller... even though it should be autoloaded.

I need help with this one. It seems that the Session Library isn't loading properly inside MY_Controller.php and I have no idea why. I get an extremely strange error that I can not seem to figure out when I try to load a function that does not exist inside a public controller that does exist. For example, http://www.mysite.com/welcome/alskdjfas will throw the error (the welcome controller exists, but that function doesn't). The Welcome controller does exist, but the "alskdjfas" function does not.

The errors say:

A PHP Error was encountered

Severity: Notice

Message: Undefined property: Welcome::$session

Filename: core/MY_Controller.php

Line Number: 14

Fatal error: Call to a member function userdata() on a non-object in C:\xampp\htdocs\websites\diftx\dol\application\core\MY_Controller.php on line 17

I don't see what I am doing wrong because the Welcome controller extends MY_Controller, and that extends CI_Controller.


class MY_Controller extends CI_Controller
var $user = FALSE;

protected $layout_view = 'main';
protected $content_view = '';
protected $view_data = array();

function __construct()
  $this->user = $this->session->userdata('USER_ID') ? User::find_by_id($this->session->userdata('USER_ID')) : FALSE;

function _output($output)
  //set the default content view
  if($this->content_view !== FALSE && empty($this->content_view)) $this->content_view = $this->router->class . '/' . $this->router->method;
  //render the content view
  $yield = file_exists(APPPATH . 'views/' . $this->content_view . EXT) ? $this->load->view($this->content_view, $this->view_data, TRUE) : FALSE;
  //render the layout
   echo $this->load->view('layouts/' . $this->layout_view, array('yield' => $yield), TRUE);
   echo $yield;

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

class Welcome extends MY_Controller {

function __construct()

function index()

function not_found()

They are extended properly (to my knowledge) and both call the constructors of the class that they extend in their own constructors with "parent::__construct()".

The Session Library is autoloaded and I use it throughout my application flawlessly but it doesn't seem to be loading in this instance, which is causing PHP errors in my application, and attempting to load the Session Library manually before where the error occurs is not fixing the problem.

My application is giving this error instead of displaying the 404 page like it should be doing. This ONLY happens when I try to load a function inside a controller that doesn't exist. The page displays the 404 page as expected if I'm trying to load a controller that doesn't exist. Only the function that doesn't exist is causing the PHP Fatal Error to happen.

Is the session library listed in your autoload config? I don't see you manually loading it in the controller, so that could explain the problem.

You have to have something wrong in your autoload, because I just tried it and it works here on CI 2.1.0 with no problem.

In your case it is not seeing the session library.

The session library is being loaded properly in the autoload. I use session and flashdata throughout my application. The only time this issue seems to be happening is when I try to load a method that doesn't exist inside a controller that does exist.

I can upload a sample of the application if you want. I have 1 app I am working on that gives the PHP errors, and another app that I am working on that doesn't give the errors, but doesn't display the custom 404 page at all. It just displays the layout (header/footer/sidebar) but where the content goes (where the "page not found" text should be) nothing happens.

I *think* I am having a problem similar to what is outlined in this thread: http://ellislab.com/forums/viewthread/178989/

Flashdata only lasts for the next server request. If that request gets used on a 404 error, you lose your flashdata.

Yeah, I know that. I only use flashdata for some things, like if a user deleted something, I'll set a flashdata variable to say "This item has been deleted" so that it's displayed on the next page. No need to keep it in a regular session variable if it's only needed for the next page. The "USER_ID" variable is not a flashdata variable, though. I have it set as a regular session variable so that the application can keep track of what user is logged in from page to page. It seems I'm not communicating my problem well enough.

I'm working on two web applications: a personal website, and an application for a client. I'll upload a working copy now of my personal website so you guys can see the files: http://skcin7.com/upload/nicholas-morgan.rar

Here is an example of the working 404 error: http://www.nicholas-morgan.com/welcomeblah (The 'welcomeblah' controller does not exist, but the 404 page shows up as expected)
Here is an example of the 404 page not showing up: http://www.nicholas-morgan.com/welcome/blah (The 'blah' method does not exist, but the 404 error does not show up. only the layout shows up)

In this project, the 404 content does not show up but the header/sidebar/footer do show up. In the project for the client, PHP Notice and Fatal Errors occur. The difference between my personal website and the one for the client is that the one for the client has a user login system, and checks the value of "USER_ID" session variable inside the constructor of the MY_Controller class, so it tries to access the Session Library which isn't loaded, so an error occurs. The Session Library isn't being loaded for some reason. The Session Library is normally autoloaded in my application, but for some reason it doesn't get autoloaded in this situation. I attempt to solve this by loading the library explicitly with $this->load->library('session') (even though I shouldn't have to) on the line before where the error is occurring, and the error still occurs. What the hell?!

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.