Quick Reference Study Notes for Codeigniter 2.x (Advanced)

  Codeigniter 2.X


Codeigniter is a PHP framework which is used for developing custom PHP based web applications. It is based on the Model-View-Controller (MVC) development pattern.

Codeigniter Config
URLs in CodeIgniter are designed to be search-engine and human-friendly. The base URL of the site configured in the application/config/config.php file.

For example: $config['base_url'] = 'http://your-domain.com';

Database Configuration
The database of the site can be configured in the application/config/database.php file. Often we need to set up the database for a different environment like development and production. Following are the basic settings that we made in the database.php file.

$db['default'] = array(
  'dsn'   => '',
  'hostname' => 'localhost',
  'username' => 'root',
  'password' => '',
  'database' => 'database_name',
  'dbdriver' => 'mysqli',
  'dbprefix' => '',
  'pconnect' => TRUE,
  'db_debug' => TRUE,
  'cache_on' => FALSE,
  'cachedir' => '',
  'char_set' => 'utf8',
  'dbcollat' => 'utf8_general_ci',
  'swap_pre' => '',
  'encrypt' => FALSE,
  'compress' => FALSE,
  'stricton' => FALSE,
  'failover' => array()


URI Segments
The segment in the URL is For example example.com/class/function/ID

Where the first segment represents the controller class, second represents the class function and the third will represents the ID or any other variable which will be passed to the controller.

To get the URI segment we use following code:


A Controller is a simple class file that is named in the way that which controls the application by whole URI. It is intermediary between the Model, View, and any other resources needed to process the HTTP request and generate a web page. Controllers exist in the application/controllers folder.

For example to create controller Blog

class Blog extends CI_Controller {
       public function index()
               echo 'Hello World!';

Visit the Url like this: example.com/index.php/blog/ it will print Hello World! On the browser screen.

A Model represents the data structure. you can insert, update information in your database with the help of Model classes. To load the model in the controller:


Models are the PHP classes that are designed to work with information in your database. The basic prototype of the model class is :

class Model_name extends CI_Model {

Where Model_name is the name of your model class.

Example for model is as follows:

class Blog_model extends CI_Model {

       public function get_last_ten_entries()
               $query = $this->db->get('entries', 10);
               return $query->result();


Views represent the information to the user. A view is simply a web page or a page fragment like header, footer. To call a view we need to load it into the controller. Views are the HTML files saved with .html extension. To load the view          



Helper as the name suggests, help you in the different tasks. Every helper file is a collection of the function used for a particular role. For example file helper, text helper, form helper etc. File helper is used to dealing with the files, the text file is used to help text formatting routines, form helper is used to creating forms. Helpers are the procedural functions independent of each other. To load the helper


To call a helper function you have to simply write this code:




Libraries are the essential part of the Codeigniter which indirectly increase the speed of developing an application. System library is located at the System/library folder. To use the library we need to load it into the controller class.


For example:$this->load->library('email');

We can create our own libraries too, which can be stored in application/libraries folder.

For example:

defined('BASEPATH') OR exit('No direct script access allowed');
class Someclass {
       public function some_method()

Now you can load it in your controller:

$this->load->library('someclass'); Once the library is loaded you can use it like


Codeigniter Forms

To use forms in the Codeigniter we use the Codeigniter form helper. To load the form helper we will use $this->load->helper('form');

For example to create forms

echo form_open('email/send');

The above example would create a form that points to your base URL and the “email/send” URI segments, like this:
<form method=”post” accept-charset=”utf-8” action=”http:..example.com/index.php/email/send”>

To create text input field

$data = array( 'name'          => 'username',
       'id'            => 'username',
       'value'         => 'johndoe',
       'maxlength'     => '100',
       'size'          => '50',
       'style'         => 'width:50%');
echo form_input($data); In this manner, you can make other input fields.

Codeigniter Sessions:

A Session is used to maintains the user state while they are browsing the website.

To initialize the session class in your controller you need to use $this->load->library('session'); in your controller constructor.

After loading the library you can use the session object like this: $this->session

To add session

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

To remove session


To fetch Session data

$name = $this->session->userdata('name');

Page caching

Caching improves the page load speed. If the page is cached, then it will be stored in its fully rendered state. Cached files are stored in the application/cache folder. Caching can be enabled in any of the controller.

$this->output->cache($n); where $n is the number of minutes, you wish the page to remain cached between refreshes.

To disable caching use this code: this->output->delete_cache();


Page Redirection

In the web applications, we need to redirect from one page to another page.

To redirect from one page to another page in Codeigniter we use redirect() function.

redirect($uri = '', $method = 'auto', $code = NULL)

$uri (string) − URI string
$method (string) − Redirect method (‘auto’, ‘location’ or ‘refresh’)
$code (string) − HTTP Response code (usually 302 or 303)

For example: to redirect to some controllers use this

redirect('redirect/computer_graphics'); where ‘redirect’ is the controller name and ‘computer_graphics’ is the function name.


To redirect to the particular site:



Common Functions

CodeIgniter uses some functions for its operation that are globally defined, and are available to you at any point. There is no need to load any libraries or helpers

For example

is_php($version): Return TRUE if the running PHP version is at least the one specified otherwise FALSE.

is_really_writable($file): Return TRUE if path is writable. etc.



XSS means cross-site scripting. CodeIgniter provides XSS filtering security. This filter will prevent any malicious JavaScript code or any other code that attempts to hijack cookie and do malicious activities. To filter data through the XSS filter, use the xss_clean() method as shown below.

$data = $this->security->xss_clean($data);

NOTE : "This study material is collected from multiple sources to make a quick refresh course available to students."

This website uses cookies to improve user experience. By using our website you consent to all cookies in accordance with our Cookie Policy. More info. I Agree