Category Archives: CodeIgniter

Codeigniter – remove index.php

Bootstrap & Apache! you can see the .htaccess

Add the following lines on your /etc/nginx/sites-enable/ or default.

# removes trailing "index" from all controllers
 if ($request_uri ~* index/?$)
 rewrite ^/(.*)/index/?$ /$1 permanent;

More details how to set up a nginx environment for CI.


Create a .htaccess file on the root of your CI installation (in the same directory where you have system, application, etc).

RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]


Awesome tutorial about mod-rewrite / pretty links


Thats kinda it!
Basically is a pretty url rewrite condition, for nginx must be also simple as that.
Simple as that!

CodeIgniter – subqueries

On my latest work/project, with CodeIgniter, I need to use subqueries – a select inside another select.

This is a subquery library for CodeIgniter’s active record class. It lets you use active record methods to create subqueries in SQL queries. It supports SELECT, JOIN, FROM (and other statements, I guess). It also supports subqueries inside subqueries.

For that I had to download a subquery library for CodeIgniter’s available where

CodeIgniter, PHP source code not compiled

I’v uploaded a CodeIgniter application from my localhost with Apache to a server running Nginx.
Its works perfectly on my localhost and on other server with Apache.
It’s under a subdomain, an domain and other subdomains are running PHP 100%.
This application in CI doesn’t start, and PHP is returned without being compiled.

This is what I get on /var/log/nginx/error.log:

2013/12/05 14:50:31 [error] 20139#0: *1 FastCGI sent in stderr: "PHP message: PHP Fatal error:  Class 'M_website' not found in /home/webroot/ on line 303" while reading upstream, client:, server:, request: "GET /websites HTTP/1.1", upstream: "fastcgi://unix:/var/run/php5-fpm.sock:", host: ""

Why the problem?

CI files were starting with


and not with



Had to edit /etc/php5/fpm/php.ini and set short_open_tag from Off to On and restart php-fpm.

service php5-fpm restart


This is how I solved it… simple issue to solve.

SQL – LEFT OUTER JOIN adding returned null values

In a SQL with LEFT OUTER JOIN I had to sum some columns but some of them returned with NULL value and the result of it was a NULL.

So imagine that we want to add table1`.`columnA`to `table2`.`columnB`, we do something like this

SELECT *, (table1`.`columnA` + `table2`.`columnB`) AS total

and if some of them is NULL the total will be NULL.

What I had to do was this

SELECT *, (ifnull(`table1`.`columnA`,0 ) + ifnull(`table2`.`columnB`, 0)) AS total

And if you want to put it on codeigniter where is how I made it:

$this->db->select('*, (ifnull(`table1`.`columnA`,0 ) + ifnull(`table2`.`columnB`, 0)) AS total', FALSE);

Note the , FALSE, it has to be there.

$this->db->select() accepts an optional second parameter. If you set it to FALSE, CodeIgniter will not try to protect your field or table names with backticks. This is useful if you need a compound select statement.

CodeIgniter – radio and checkbox – value=”0″


In a work that i’m working on, some values of checkboxes and radios have values = 0.
CodeIgniter don’t like them.

Found the solution for it here.

FYI – I fixed this bug. I dont know how to do pull requests – but you need to change Form_validation line 825:

from: if (($field == ” OR $value == ”) OR ($field != $value))
to: if (($field === ” OR $value === ”) OR ($field != $value))

Otherwise it doesnt evaluate the 0 correctly.

You probably also need to do it to lines 781 and 869 (set_select() and set_checkbox() )- but I havent tested it

By TheShiftExchange

CodeIgniter, dompdf-master and php-font-lib

In a project for a non-lucrative organization, we need to implement a print to pdf.

rock ssd

A PHP Error was encountered

Severity: Warning Message: require_once(/home/creaftpu/subdomains/stocks/system/helpers/dompdf/lib/php-font-lib/classes/font.cls.php) [function.require-once]: failed to open stream: No such file or directory Filename: dompdf/ Line Number: 335 Fatal error: require_once() [function.require]: Failed opening required '/home/creaftpu/subdomains/stocks/system/helpers/dompdf/lib/php-font-lib/classes/font.cls.php' (include_path='.:/usr/lib/php:/usr/local/lib/php') in/home/creaftpu/subdomains/stocks/system/helpers/dompdf/ on line 335

We will use dompdf in codeigniter BUT the file that I’v downloaded from GitHub doesn’t has all the files that we need. It’s missing all the files of php-font-lib. So we had to download it and uploaded to the respective folder, to, to have dompdf working in codeigniter you might have to download php-font-lib.

How to close session when browser is closed, in CodeIgniter?

We are building a small app for a non-lucrative, and we are using sessions on it – who doesn’t use it? -.
We need that when some user closes the browser the session must be destroyed.

This is how its done.

Open the geral config file application/config/config.php, search for $config[‘sess_expiration’]

$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 7200;
$config['sess_expire_on_close']	= FALSE;
$config['sess_encrypt_cookie']	= FALSE;
$config['sess_use_database']	= FALSE;

Change 7200 for -1. You should got something like

$config['sess_expiration'] = -1;

That should do the work! 🙂