5 Things You May Not Know About CodeIgniter

Every time I start a new project with CodeIgniter I find myself tasked with something, often a small thing, that I’ve never “solved” before. I try to use these moments as opportunities to explore PHP and CodeIgniter a little bit deeper. Often times I surprise myself by learning something new, or surprise myself at having forgotten something relatively “basic”. Here are 5 things I’ve discovered or rediscovered in the last few years. If you’re a CI veteran, some of these will be old news to you, and some might be new. Maybe just like me you’ve forgotten about a handy trick. Regardless, they’ve become mainstays in my coding now, and I think they deserve a bit more recognition.

1) There is an optional second parameter in $this->uri->segment()

uri_segment() is one my most frequently used functions to move information from page to page. After all, how many times have you seen vs in action? In these cases, we often use

$robot $this->uri->segment(3); 

There is an optional second parameter that permits you to set your own default value if the segment is missing. For example, we could use

$robot $this->uri->segment(3'calculon'); 

This allows you to grab a specific robot out of the url, but if there is no specific robot, simply default on Calculon.

2) Any library can have a configuration file

I try to keep my library configuration options out of my controllers where possible. Fortunately CodeIgniter lets us seamlessly move that information into application/config without missing a step. Simply name your configuration file the same as the library and you’re set. My favourite for this trick is email library. Here’s the application/config/email.php file I include in every bit of code I distribute.

<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

* If you are using this app in a professionally hosted environment, you probably don't need
* to do anything to this file.  If you are running it from your home server, or have a setup
* on a pre-configured server setup, such as WinLAMP or XXAMP, then you'll likely
* need to uncomment the configuration information below.
* Check with your ISP for this information.  It is usually given in the welcome information
* when you sign up for the service, or can likely be retrieved from your email package of choice.

$config['protocol'] = 'smtp';
$config['smtp_host'] = '';
$config['smtp_user'] = 'username';
$config['smtp_pass'] = 'password';
$config['smtp_port'] = '25';

/* End of file email.php */ 

The third parameter allows you to suppress errors in the event that a config file does not exist:


3) remove_invisible_characters()

Here’s a handy little guy.

This function prevents inserting null characters between ascii characters, like Java\0script.

Until recently, I’d forgotten all about this little Common Function. (edit: fixed another link, thanks Ossama)

Generally speaking, you’ll probably be using XSS protection in any place you need this (in fact, CodeIgniter’s xss_clean() function actually calls remove_invisible_characters()), but if you find yourself in a situation where XSS cleaning isn’t warranted, then remove_invisible_characters() might be the way to go.

4) CodeIgniter 2 allows for libraries to have drivers

This isn’t really an example of “oh hey I forgot about that tip” so much as it is an awesome new CI2 feature that isn’t getting the recognition I feel it deserves. The documentation could use a little fleshing out still, but check out Using CodeIgniter Drivers and Creating Drivers. (edit: fixed those links… thanks Adam)

5) View’s can be returned as data with a third parameter

I love this option and can think of two places I’ve used it with great success; building PDFs and constructing downloads.

$page $this->load->view('view'$dataTRUE);
$pdf build_pdf($page);

// OR

$view_data $this->load->view('xml_template'$dataTRUE);


I’m always impressed at the flexibility of my tools, and here are 5 admittedly simple tricks that have given me more “bang for the buck” over the years. What are your favourite tips? Please leave them in the comments!

This entry was made on and filed into CodeIgniter.


Eric Barnes wrote on

Just to add one more to the list :) config_item(‘string’); is one I seemed to have overlooked until it was recently pointed out to me. In the docs it is hidden away but very useful to use in your view files.

Adam Fairholm wrote on

Hey Derek - the driver links are linking to the CI 1.7.3 docs that don’t have the driver info. The drivers feature is great, though.

Derek wrote on

Opps! Thanks Admin Adam (god I’m an idiot). Fixed up!

Adam Fairholm wrote on

I’m the coolest admin ever!

Just wanted to interject my favorite CI nugget:

Doesn’t come up a lot, but I sometimes use the Database feature that lets you pass a DSN string. IE:


It’s in the docs but I don’t think it is that widely known. Comes in handy once in a blue moon!

Ossama Khayat wrote on

Indeed. We usually forget about the obvious and try hard to find solutions.
Two notes:
1. The uri_segment() documentation link shows no second parameter in the definition, though it’s there in the example.
2. remove_invisible_characters() isn’t even there in the ‘Common Functions’ in the link provided. Is it a CI2.0 function?

Derek wrote on

Thanks Ossama, link fixed. I wrote most of the article when I was offline and using local references… that’ll teach me a lesson :)

Yahya wrote on

Thanks Derek Allard.
Php ~ Codeigniter ~ MySQL ~ Xhtml ~ CSS ~ XML ~ Jquery

Matthew Fedak wrote on

I recently built a little CRM in codeigniter and one of my models was 1000’s of lines long because it had to send lengthy HTML format emails but once I learned how to store the email templates in view files and then just load them into a mailbody variable to pass to the codeigniter mail function. It really helped me organise things easily. The more semantic the better!

Rohrreinigung Köln wrote on

Nice to see your tips here posted! They are really helpful for newbies - a total must have!

Boris K wrote on

1. I start a project by moving the applications directory out of system (mv applications ..), so they are siblings. You don’t have to fix anything as CI finds it in its new place.

2. I install Matchbox to deal with modules.

SenthilguruE wrote on

Hey Derek ,

Thanks for the info, we are working on Open Source video product using CI as core script.

The info provided by you would be very helpful for that.

sabarinathan wrote on

first of all my self i am student.i am new to PHP.i like to develop webpage using code igniter.i am using os as windows xp,apache, PHP 5.3,my database is Oracle.i need some guide to config the oracle database.And how to work with oracle database.

Derek wrote on

@sabarinathan: To get CodeIgniter support, hit up the forums.

Tim Cason wrote on

Thanks! I just started using CI 2 and have been upgrading my sites.  Several of these tips (URI default, lib config) were definitely added on the upgrade.

Anonymous Coward wrote on

I am new in codeigniter programming. This post help me a lot.thanks for posting it.