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 9135231
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:47:50+00:00 2026-06-17T08:47:50+00:00

Possible Duplicate: echo user in view from sessions code igniter I don’t want to

  • 0

Possible Duplicate:
echo user in view from sessions code igniter

I don’t want to define and store the user in every controller and then pass it to the view.
Here’s my controller:

Login controller:

class LoginController extends CI_Controller {
    function index(){       
        $new['main_content'] = 'loginView';
        $this->load->view('loginTemplate/template', $new);          
    }

    function verifyUser(){

    //getting parameters from view 
    $data = array(
            'username' => $this->input->post('username'),
            'password' => $this->input->post('password')
    );      

    $this->load->model('loginModel'); 
    $query = $this->loginModel->validate($data);

    if ($query){             
        //if the user c validated data variable is created becx we want to put username in session

            $data = array(
                'username' => $this->input->post('username'),
                 'is_logged_in' => true 
            );

           $this->session->set_userdata($data);
           redirect('sessionController/dashboard_area');
    }else{
        $this->index();
    }
}
function logout()
{
    $this->session->sess_destroy();
    $this->index();
}
}



?>

My controller, which I’ve stored in core folder, so now every controller now extends this controller. I think this controller can be customize so I can access the user in every view page which extended this controller:

   class MY_Controller extends CI_Controller{
        function __construct(){
    parent::__construct();
    $this->is_logged_in();

    }   
    function dashboard_area(){
    $data['main_content'] = 'dashboardView';
    $this->load->view('dashboardTemplate/template', $data);         
    }

function is_logged_in()
{
    $is_logged_in = $this->session->userdata('is_logged_in');

    if(!isset($is_logged_in) || $is_logged_in != true)
    {
        echo 'You don\'t have permission to access this page.';
        redirect('loginController');
    }
}


           }
?>

Here is my simple one member controller which extended the above controller:
Here in index function I am storing the username and then pass into the view which I don’t want to do:

     class CategoryController extends MY_Controller {
   function index(){         
    $data['main_content'] = 'categoryView';         
    $username= $this->session->userdata('username');
    $data['username']=$username;
    $this->load->view('dashboardTemplate/template',$data);
      }
  • 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-17T08:47:50+00:00Added an answer on June 17, 2026 at 8:47 am

    You can just call $this->session->userdata('username') in your view.

    It is stored in the session, so you do not have to pass it to the views from the controller.

    UPDATE PER COMMENT;
    if you want to load a view depending on the base controller (eg user), I would use a template library and set the template to use in the construct of the base controller.

    For example (using this template library);

    class MY_User extends CI_Controller {
       public __construct() {
          parent::__construct();
          $this->is_logged_in();
          $this->template->set_template('user');
       }
    }
    
    class MY_Admin extends CI_Controller {
       public __construct() {
          parent::__construct();
          $this->is_logged_in();
          $this->template->set_template('admin');
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: How to save user-entered line breaks from a TEXTAREA to a database?
Possible Duplicate: Function to get user ip address <?PHP $ipaddress = $_SERVER[REMOTE_ADDR]; echo Your
Possible Duplicate: How do i echo a Resource id #6 from a MySql response
Possible Duplicate: Writing a function in php I'm using the following code echo 'Curl:
Possible Duplicate: Strip all HTML tags, except allowed I have this code: <?php echo
Possible Duplicate: Phonegap - Load Values from Database based on a user ID I
Possible Duplicate: How to get file name from full path with PHP? echo '<td
Possible Duplicate: && operator in Javascript In the sample code of the ExtJS web
Possible Duplicate: How can I convert a list<> to a multi-dimensional array? I want
Possible Duplicate: How to do the vector of sets in C++? I want to

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.