Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6327499
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T17:17:27+00:00 2026-05-24T17:17:27+00:00

I have created a MY_Controller class to check for sessions. My LoginController is checking

  • 0

I have created a MY_Controller class to check for sessions.

My LoginController is checking the user information and if that is ok, I redirect the user to the PainelController. I use redirect so my url will be refresh with /localhost/painel instead of /localhost/session/login

The problem is that when I use redirect, I cannot access my session, only using load->view.

Is there a workaround?

Thanks in advance for any help.

PS: I use a .htaccess, the one found on CI Wiki

EDIT

class MY_Controller extends CI_Controller {

    public function __construct() {
        parent::__construct();
        if(!$this->session->userdata('usuario')) {
            redirect('login');
        }
    }

}

Piece of Login: extends CI_Controller

if( $rs )
            {

                $this->session->set_userdata('usuario', $usuario);//usuario is a object
                //$this->load->view('painel');//it works
                redirect('painel', 'location');//it doesn't
            }
            else
            {
                $this->load->view('login', $data = array('mensagem'=>'Usuário ou senha inválidos.'));
            }

—

My Painel View

echo $this->session->userdata('usuario')->usuario_nome; //it works
only if I load->view('painel')

Even if I try to access this session value on my PainelController (extends MY_Controller) it won’t work, will say:

Message: main() [function.main]: The script tried to execute a method or access a property of an incomplete object. Please ensure that the class definition "Usuario" of the object you are trying to operate on was loaded _before_ unserialize() gets called or provide a __autoload() function to load the class definition
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T17:17:27+00:00Added an answer on May 24, 2026 at 5:17 pm

    I tried redirecting a couple of times on my CI sandbox but I didn’t lose my session.
    What libraries/helpers are you using in PainelController?
    It seems you don’t have @session_start(); in that particular controller.

    Usually, you can use existing Auth libraries that will handle this for you, and then include this in the constructor of every controller that or even in MY_Controller.
    Here’s an example:

    class Events extends CI_Controller {
    // Constructor function
    public function __construct()
    {
        //load initial models and libraries that are needed for the controller
        parent::__construct();
        $this->load->library('auth');
                ...
        }
        ...
    }
    

    And then my auth library would have a constructor like this.

    class Auth {
      var $CI       = NULL;
    
      function Auth($props = array())
      {
        $this->CI =& get_instance();
    
        // Load additional libraries, helpers, etc.
        $this->CI->load->library('session');
        $this->CI->load->database();
        $this->CI->load->helper('url');
        @session_start();
      }
      ...
    }
    

    EDIT:

    You can also include this in your autoload usually located at config/autoload.php.
    http://codeigniter.com/user_guide/general/autoloader.html

    Here’s an example.

    /*
    | -------------------------------------------------------------------
    |  Auto-load Libraries
    | -------------------------------------------------------------------
    | These are the classes located in the system/libraries folder
    | or in your application/libraries folder.
    |
    | Prototype:
    |
    |   $autoload['libraries'] = array('database', 'session', 'xmlrpc');
    */
    
    $autoload['libraries'] = array('database','session', 'encrypt');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a BasicMembershipProvider class that inherits from MembershipProvider, when implemented, the ValidateUser
I have created my own Attached Property like this: public static class LabelExtension {
I have created my custom MembershipProvider. I have used an instance of the class
I'm trying to place a control that I have created on my Canvas .
I am trying to extend the CodeIgniter controller. I have created MY_Controller.php file, the
I have a User & Profile Models. A user has_one profile IE. class User
I have created a model POCO class called Recipe ; a corresponding RecipeRepository persists
I have a data validation class that checks whether the start date of a
I have created my own Tree implementation for various reasons and have come up
i have created folder on my server (ie finesse)- 'home' in which i have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.