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

  • Home
  • SEARCH
  • 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 8135507
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T10:26:48+00:00 2026-06-06T10:26:48+00:00

I have a controller where in the constructor function, I want to check if

  • 0

I have a controller where in the constructor function, I want to check if the user is logged in or not. If not, I want an error message to be displayed, and for the script to exit without running any other function in the controller. This controller will only be called by ajax so the error would be displayed via JSON and then the javascript on the client will display it to the user.

How can I do this? If I did this:

function __construct()
{
   if (! $this->loggedIn() )
   {
      echo json_encode( array('error'=> true) );
      die;
   }    
}

I don’t think the message would be displayed because codeigniter uses output buffering. Any ideas?

  • 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-06-06T10:26:49+00:00Added an answer on June 6, 2026 at 10:26 am

    i understood that your problem is the client expects for a json type of response, so two options to use:

    public function __construct(){
    
        $_bad_login_msg = 'please try again' ;
        parent::__construct();
    
        if(!userLoggedIn()){
    
            $this->output
            ->set_content_type('application/json')
            ->set_output(json_encode($_bad_login_msg));
    
            //or just use 
            //     echo json_encode($_bad_login_msg);
            die;
        }
    }
    

    http://codeigniter.com/user_guide/libraries/output.html

    you won’t have any buffering problems, the buffer contents will be sent to the client after the die…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have controller with action new , and I want it to create ActiveRecord::Base
I have a CodeIgniter project, and I want to invoke one of my controller
I have two functions in controller named step1 and step2. I want to pass
I have a model which throws a XMLDocument_Exception. In my controller action I want
I am attempting to create a constructor for my controller that references a function
I have a model where I want to translate the form validation error. So
I have a controller called user which just loads the user profile page for
This is what I have: class Calendar extends CI_Controller { public $extension; function __construct()
I have controller PlayerController and actions inside: View , Info , List . So
I have Controller: [MySite]\Controllers\DistributionTools\TrackingChannelsController.cs [HttpPost] public void InitTcFirstPageView() { var model = new TcFirstPageModel

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.