CodeIgniter User Guide
Welcome to CodeIgniter
CodeIgniter is an Application Development Framework
for people who build
web sites using PHP. Its goal is to enable you to develop projects much faster than you
could if you were writing code f
rom scratch, by providing a rich set of libraries for
commonly needed tasks, as well as a simple interface and logical structure to access these
libraries. CodeIgniter lets you creatively focus on your project by minimizing the amount
of code needed for a
Who is CodeIgniter For?
CodeIgniter is right for you if:
You want a framework with a small footprint.
You need exceptional performance.
You need broad compatibility with standard hosting accounts that run a variety of
PHP versions and configura
You want a framework that requires nearly zero configuration.
You want a framework that does not require you to use the command line.
You want a framework that does not require you to adhere to restrictive coding
You are not interested in lar
scale monolithic libraries like PEAR.
You do not want to be forced to learn a templating language (although a template
parser is optionally available if you desire one).
You eschew complexity, favoring simple solutions.
You need clear, thorough document
version 4.3.2 or newer.
A Database is required for most web application programming. Current supported
databases are MySQL (4.1+), MySQLi, MS SQL, Postgres, Oracle, SQLite, and
CodeIgniter License Agreement
Copyright (c) 2008
2009, EllisLab, Inc.
All rights reserved.
This license is a legal agreement between you and EllisLab Inc. for the use of
CodeIgniter Software (the "Software"). By obtaining the Software you agree to c
with the terms and conditions of this license.
You are permitted to use, copy, modify, and distribute the Software and its
documentation, with or without modification, for any purpose, provided that the
following conditions are met:
opy of this license agreement must be included with the distribution.
Redistributions of source code must retain the above copyright notice in all source
Redistributions in binary form must reproduce the above copyright notice in the
ion and/or other materials provided with the distribution.
Any files that have been modified must carry notices stating the nature of the
change and the names of those who changed them.
Products derived from the Software must include an acknowledgment that
are derived from CodeIgniter in their documentation and/or other materials
provided with the distribution.
Products derived from the Software may not be called "CodeIgniter", nor may
"CodeIgniter" appear in their name, without prior written permissio
You agree to indemnify and hold harmless the authors of the Software and any
contributors for any direct, indirect, incidental, or consequential third
actions or suits, as well as any related expenses, liabilit
ies, damages, settlements or fees
arising from your use or misuse of the Software, or a violation of any terms of this
Disclaimer of Warranty
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESSED OR IMPLIED, INCLUDING, BUT NOT
LIMITED TO, WARRANTIES
OF QUALITY, PERFORMANCE, NON
OR FITNESS FOR A PARTICULAR PURPOSE.
Limitations of Liability
YOU ASSUME ALL RISK ASSOCIATED WITH THE INSTALLATION AND USE
OF THE SOFTWARE. IN NO EVENT SHALL THE AUTHORS OR
HOLDERS OF THE SOFTWARE BE LIABLE FOR CLAIMS, DAMAGES OR
OTHER LIABILITY ARISING FROM, OUT OF, OR IN CONNECTION WITH THE
SOFTWARE. LICENSE HOLDERS ARE SOLELY RESPONSIBLE FOR
DETERMINING THE APPROPRIATENESS OF USE AND ASSUME ALL RISKS
ITH ITS USE, INCLUDING BUT NOT LIMITED TO THE RISKS
OF PROGRAM ERRORS, DAMAGE TO EQUIPMENT, LOSS OF DATA OR
SOFTWARE PROGRAMS, OR UNAVAILABILITY OR INTERRUPTION OF
CodeIgniter was originally developed by
framework was written for performance in the real world, with many of the class
libraries, helpers, and sub
systems borrowed from the code
It is currently developed and maintained by the ExpressionEngine Development Team.
A hat tip goes to Ruby on Rails for inspiring us to create a PHP framework, and for
bringing frameworks into the general consciou
sness of the web community.
CodeIgniter is installed in four steps:
Unzip the package.
Upload the CodeIgniter folders and files to your server. Normally the index.php
file will be at your root.
file with a text editor and set your base
URL. If you intend to use encryption or sessions, set your encryption key.
If you intend to use a database, open the
a text editor and set your database set
If you wish to increase security by hiding the location of your CodeIgniter files you can
folder to something more private. If you do rename it, you must open
file and set the
variable at the top
of the page
with the new name you've chosen.
If you're new to CodeIgniter, please read the
section of the User Guide to
begin learning how to build d
ynamic PHP applications. Enjoy!
If you find that no matter what you put in your URL only your default page is loading, it
might be that your server does not support the PATH_INFO variable needed to serve
engine friendly URLs. As a fi
rst step, open your
and look for the
information. It will recommend that you try a couple
alternate settings. If it still doesn't work after you've tried this you'll need to force
CodeIgniter to add a questio
n mark to your URLs. To do this open your
file and change this:
$config['index_page'] = "index.php";
$config['index_page'] = "index.php?";
Getting Started With CodeIgniter
Any software application requi
res some effort to learn. We've done our best to minimize
the learning curve while making the process as enjoyable as possible.
The first step is to
CodeIgniter, then read a
ll the topics in the
of the Table of Contents.
Next, read each of the
pages in order. Each topic builds on the previous
one, and includes code examples that you are encouraged to try.
Once you understand the basics you'l
l be ready to explore the
pages to learn to utilize the native libraries and helper files.
Feel free to take advantage of our
if you have questions or prob
to see code examples posted by other users.
CodeIgniter at a Glance
CodeIgniter is an Application Framework
CodeIgniter is a toolkit for people who build web application using PHP. Its goal is
enable you to develop projects much faster than you could if you were writing code from
scratch, by providing a rich set of libraries for commonly needed tasks, as well as a
simple interface and logical structure to access these libraries. CodeIgniter
creatively focus on your project by minimizing the amount of code needed for a given
CodeIgniter is Free
CodeIgniter is licensed under an Apache/BSD
style open source license so you can use it
however you please. For more information please
CodeIgniter Runs on PHP 4
CodeIgniter is written to be compatible with PHP 4. Although we would have loved to
take advantage of the better object handling in PHP 5 s
ince it would have simplified
some things we had to find creative solutions for (looking your way, multiple
inheritance), at the time of this writing PHP 5 is not in widespread use, which means we
would be alienating most of our potential audience. Major O
S vendors like RedHat are
moving slowly to support PHP 5, and they are unlikely to do so in the short term, so we
felt that it did not serve the best interests of the PHP community to write CodeIgniter in
Note: CodeIgniter will run on PHP 5. It simp
ly does not take advantage of any native
features that are only available in that version.
CodeIgniter is Light Weight
Truly light weight. The core system requires only a few very small libraries. This is in
stark contrast to many frameworks that require s
ignificantly more resources. Additional
libraries are loaded dynamically upon request, based on your needs for a given process,
so the base system is very lean and quite fast.
CodeIgniter is Fast
Really fast. We challenge you to find a framework that has
better performance than
CodeIgniter Uses M
CodeIgniter uses the Model
Controller approach, which allows great separation
between logic and presentation. This is particularly good for projects in which designers
are working with your t
emplate files, as the code these file contain will be minimized.
We describe MVC in more detail on its own page.
CodeIgniter Generates Clean URLs
The URLs generated by CodeIgniter are clean and search
engine friendly. Rather than
using the standard "query
string" approach to URLs that is synonymous with dynamic
systems, CodeIgniter uses a segment
Note: By default the index.php file is included in the URL but it can be removed using a
simple .htaccess file.
gniter Packs a Punch
CodeIgniter comes with full
range of libraries that enable the most commonly needed
web development tasks, like accessing a database, sending email, validating form data,
maintaining sessions, manipulating images, working with XML
data and much
CodeIgniter is Extensible
The system can be easily extended through the use of plugins and helper libraries, or
through class extensions or system hooks.
CodeIgniter Does Not Require a Template Engine
a simple template parser that can be optionally
used, it does not force you to use one. Template engines simply can not match the
performance of native PHP, and the syntax that must be learned to use a template engine
is usually only marginally easier tha
n learning the basics of PHP. Consider this block of
<?php foreach ($addressbook as $name):?>
<?php endforeach; ?>
Contrast this with the pseudo
code used by a template engine:
Yes, the template engine example is a bit cleaner, but it comes at the price of
performance, as the pseudo
code must be converted back into PHP to run. Since one of
our goals is
, we opt
ed to not require the use of a template engine.
CodeIgniter is Thoroughly Documented
Programmers love to code and hate to write documentation. We're no different, of course,
but since documentation is
as the code itself, we are committed to do
Our source code is extremely clean and well commented as well.
CodeIgniter has a Friendly Community of Users
Our growing community of users can be seen actively participating in our
Features in and of themselves are a very poor way to judge an application since they tell
you nothing about the user experience, or how intuitively or intelligently it is designed.
Features don't reveal anything about the quality of the code, or the
performance, or the
attention to detail, or security practices. The only way to really judge an app is to try it
and get to know the code.
CodeIgniter is child's play so we encourag
e you to do
just that. In the mean time here's a list of CodeIgniter's main features.
Controller Based System
PHP 4 Compatible
Extremely Light Weight
Full Featured database classes with support for several platforms.
Active Record Database Suppo
Form and Data Validation
Security and XSS Filtering
Email Sending Class. Supports Attachments, HTML/Text email, multiple
protocols (sendmail, SMTP, and Mail) and more.
Image Manipulation Library (cropping, resizing, rotating, etc.). S
ImageMagick, and NetPBM
File Uploading Class
Full Page Caching
User Agent Class
Zip Encoding Class
Unit Testing Class
engine Friendly URLs
Flexible URI Routing
Support for Hooks, Class Extensions, and Plugins
Large library of "helper" functions
Application Flow Chart
The following graphic illustrates how data
flows throughout the system:
The index.php serves as the front controller, initializing the base resources needed
to run CodeIgniter.
The Router examines t
he HTTP request to determine what should be done with it.
If a cache file exists, it is sent directly to the browser, bypassing the normal
Security. Before the application controller is loaded, the HTTP request and any
user submitted data
is filtered for security.
The Controller loads the model, core libraries, plugins, helpers, and any other
resources needed to process the specific request.
The finalized View is rendered then sent to the web browser to be seen. If caching
is enabled, the
view is cached first so that on subsequent requests it can be served.
By default, URLs in CodeIgniter are designed to be search
engine and human friendly.
Rather than using the standard "query string" approach to URLs that is synonymous w
dynamic systems, CodeIgniter uses a
Query string URLs can be optionally enabled, as described below.
The segments in the URL, in following with the Model
The first segment represents the controller
that should be invoked.
The second segment represents the class
, or method, that should be
The third, and any additional segments,
represent the ID and any variables that
will be passed to the controller.
n functions that make it easy to work with your
URI data. In addition, your URLs can be remapped using the
Removing the index.php file
file will be included in your URLs:
You can easily remove this file by using a .htaccess file with some simple rules. Here is
an example of such a file, using the "negative" method in which e
verything is redirected
except the specified items:
RewriteCond $1 !^(index
RewriteRule ^(.*)$ /index.php/$1 [L]
In the above example, any HTTP request other than those for index.php, images, and
robots.txt is tre
ated as a request for your index.php file.
Adding a URL Suffix
file you can specify a suffix that will be added to all URLs
generated by CodeIgniter. For example, if a URL is this:
n optionally add a suffix, like
, making the page appear to be of a certain
Enabling Query Strings
In some cases you might prefer to use query strings URLs:
Igniter optionally supports this capability, which can be enabled in your
file. If you open your config file you'll see these items:
$config['enable_query_strings'] = FALSE;
$config['controller_trigger'] = 'c';
ger'] = 'm';
If you change "enable_query_strings" to TRUE this feature will become active. Your
controllers and functions will then be accessible using the "trigger" words you've set to
invoke your controllers and methods:
If you are using query strings you will have to build your own URLs, rather
than utilizing the URL helpers (and other helpers that generate URLs, like some of the
form helpers) as these are designed to work with segment based URLs.