CodeIgniter Email Configuration File

Turns out that while you can have an email configuration file (under application/config/email.php), this file will NOT work if you put into environment specific folders (e.g. application/config/development/email.php).

To make it work in such cases (e.g. for MockSMTP), you’ll instead need to check the environment and use the appropriate configuration. 

Here’s an example of what to put in the file:

if (ENVIRONMENT === 'development') {
    $config['protocol'] = 'smtp';   
    $config['smtp_port'] = 1025;    
else { // Production and other servers
    $config['protocol'] = 'smtp';   
    $config['smtp_port'] = 25; 

$config['smtp_host'] = 'localhost'; 
$config['charset'] = 'iso-8859-1';
$config['wordwrap'] = TRUE;

Disallowed Key Characters Error

I loaded a CodeIgniter site today and received a blank page with just the message “Disallowed Key Characters”.

There are myriad of scenarios that can cause this to occur. In my particular example, it occurred due to the fact that my application/config.php file had the following set:

$config['cookie_prefix'] = "<CHANGEME>_";

I had put that in as a placeholder, but the “<” and “>” symbols are not allowed in this particular config value and throw this error. There are other symbols that will also cause this, so something to watch out for.

After you change this, remember to clear your browser cookies too.

Should you use frameworks?

One of the best things about the CodeIgniter PHP framework is the wide range of classes and helpers available, everything you need to simplify working with URLs, forms, database connections, email, strings, XML, sessions and security among many, many others. 

But should you use such frameworks? I believe you should, and my reasoning is this:

“I’ll never write code that is as bug-free, secure and efficient as a community of developers in a framework.”

Many programmers fall into the trap that they think they can write code better than all those that have come before them …

Luckily these days most new programmers now learn a language and then a framework almost immediately helping to mitigate this syndrome and promote good code use.

It all comes down to how honest you are about your skills. Remember, for every few lines of code you write, you have more than likely introduced a new bug.

Combine anchor and img in CodeIgniter

CodeIgniter has a number of cool URL helper functions, two of them are anchor and img which generate the anchor and img HTML tags for you.

What’s even better though is you can combine these like so:

echo anchor('', img(''));

Which produces the combined image with a link anchor around it.

CodeIgniter Last Inserted ID

CodeIgniter comes with a number of cool database helpers, like $this->db->insert_id() which tracks the automatic ID created for the last insert statement you ran so you can use it in subsequent code that relies on that ID.

For example:

$result = $this->db->insert('users', $user_data);

$user_id = $this->db->insert_id();

Now you have the user ID from the insert which can you then work with in your code without having to go back to the database and look it up.

CodeIgniter Default Controller Routing

In CodeIgniter you use application/config/routes.php to set up the default controller for routing (the controller that automatically takes over when one isn’t specified) for example:

$route['default_controller'] = "user";

This is a good start, but the URL still needs to specify the controller when accessing functions outside of index. For example the URL needs to be: http://localhost/user/login instead of simply http://localhost/login to get to the user login function.

I haven’t come across an elegant fix for this. Things like using the $route[’(:any)’] parameter tends to break other controllers.

But what you can do is simply create a route for every matching function in your default controller. E.g.:

$route['login'] = 'user/login';
$route['logout'] = 'user/logout';

One caveat is if you have a function that matches another controller name you’ll need to change either the function name or the conflicting controller name.