CodeIgniter Interview Questions

CodeIgniter Interview Questions Download CodeIgniter Interview Questions PDF

CodeIgniter Interview Questions

Sessions in CodeIgniter

In CodeIgniter Session class allows you maintain a user’s “state” and track their activity while they are browsing your website.
In order to use session, you need to load Session class in your controller.

$this->load->library(‘session’); method is used to sessions in CodeIgniter


Once loaded, the Sessions library object will be available using:


Reading session data in CodeIgniter

Use $this->session->userdata(); method of session class to read session data in CodeIgniter.



You can also read a session data by using the magic getter of CodeIgniter Session Class



Where an item is the name of the key you want to retrieve.

Creating a session in CodeIgniter

Session’s Class set_userdata() method is used to create a session in CodeIgniter. This method takes an associative array containing your data that you want to add in session.


$newdata = array(
        'username'  => 'johndoe',
        'email'     => '',
        'logged_in' => TRUE

If you want to add userdata one value at a time, set_userdata() also supports this syntax:

$this->session->set_userdata('some_name', 'some_value');

Removing Session Data

Session’s Class unset_userdata() method is used to remove a session data in CodeIgniter. Below are example usages of same.

Unset particular key


Unset an array of item keys

 $array_items = array('username', 'email');

Read More

In Codeigniter, models will typically be loaded and called from within your controller methods. To load a model you will use the following method:


If your model is located in a sub-directory, include the relative path from your model’s directory. For example, if you have a model located at application/models/blog/Posts.php you’ll load it using:


Once your Model loaded, you will access its methods using an object with the same name as your controller:


class Blog_controller extends CI_Controller {
        public function blog()

                $data['query'] = $this->blog->get_last_ten_entries();

                $this->load->view('blog', $data);

Follow below steps to index.php from URL in Codeigniter

  • Step 1: Open config.php and replaces
    $config['index_page'] = "index.php" to $config['index_page'] = "" 
    $config['uri_protocol'] ="AUTO" to $config['uri_protocol'] = "REQUEST_URI"
  • Step 2: Change your .htaccess file to
    RewriteEngine on
    RewriteCond $1 !^(index\.php|[Javascript / CSS / Image root Folder name(s)]|robots\.txt)
    RewriteRule ^(.*)$ /index.php/$1 [L]

In Codeigniter, you can link images/CSS/JavaScript by using the absolute path to your resources.

Something like below

// References your $config['base_url']
<img src="<?php echo site_url('images/myimage.jpg'); ?>" />

CodeIgniter uses a few functions for its operation that are globally defined, and are available to you at any point. These do not require loading any libraries or helpers.

  • is_php($version)
  • is_really_writable($file)
  • config_item($key)
  • set_status_header($code[, $text = ”])
  • remove_invisible_characters($str[, $url_encoded = TRUE])
  • html_escape($var)
  • get_mimes()
  • is_https()
  • is_cli()

Read Best 80+AngularJS Interview Questions

CodeIgniter’s Hooks feature provides a way to modify the inner workings or functionality of the framework without hacking the core files.

The following is a list of available hook points.

  • pre_system Called very early during system execution.
  • pre_controller Called immediately prior to any of your controllers being called.
  • post_controller_constructor Called immediately after your controller is instantiated, but prior to any method calls happening.
  • post_controller Called immediately after your controller is fully executed.
  • display_override Overrides the _display() method.
  • cache_override Enables you to call your own method instead of the _display_cache() method in the Output Library. This permits you to use your own cache display mechanism.
  • post_system Called after the final rendered page is sent to the browser, at the end of system execution after the finalized data is sent to the browser.

Read More

As the name suggests, helpers help you with tasks.Each helper file is simply a collection of functions in a particular category.They are simple, procedural functions.Each helper function performs one specific task, with no dependence on other functions.

CodeIgniter does not load Helper Files by default, so the first step in using a Helper is to load it. Once loaded, it becomes globally available in your controller and views.

Helpers are typically stored in your system/helpers, or application/helpers directory

Loading a helper file is quite simple using the following method:


Where name is the file name of the helper, without the .php file extension or the “helper” part.
Read More

  • Framework with a small footprint
  • Simple solutions over complexity
  • Clear documentation
  • Exceptional performance
  • Strong Security
  • Nearly zero configuration

All Routing rules in Codeigniter are defined in your application/config/routes.php file.

In Codeigniter by default all config variables are located at “application/config/config.php” file.
Below is the way to set or get a config variable in Codeigniter
// Setting a config variable dynamically
$this->config->set_item('variable_name', value);
// Getting value of config variable in Codeigniter.
In Codeigniter, delete function is used to delete the one or more row data from a table.
//DELETE FROM table WHERE id = $id
$conditions =['id' => $id]
$this->db->delete('table_name', $conditions); 

// Deleting records from more than one tables in one go
$tables = array('table1', 'table2', 'table3');
$this->db->where('id', $id);
In CodeIgniter, URLs are designed to be search-engine and human-friendly.CodeIgniter uses a segment-based approach rather than using “query string” based approach.

The default URL pattern in CodeIgniter consists of 4 main components. They are :

  1. A server name (
  2. A Controller (user)
  3. An Action or method (edit)
  4. An optional action parameter (ramesh)

Prepare 10  Essential Magento Interview Questions

order_by function is used to order the records from a table in CodeIgniter.
// Getting random rows from database in CodeIgniter
$this->db->order_by("column_name", "random");
$result = $this->db->get()->result();