Fixing CodeIgniter Email Setup

I have been using an application I wrote in CodeIgniter that sends email notifications when an advertising link is activated or when it has expired. Several weeks ago, it suddenly stopped working. It was no longer sending email notifications as intended. I was using GMail’s SMTP server to send out the email notifications. GMail’s SMTP does require authentication. Typically, the CodeIgniter’s email preferences are located in the “config/email.php” file. It dawned on me that changing my GMail password broke my script. Here’s my email preferences found in the config/email.php file.

$config['protocol']='smtp';
$config['smtp_host']='ssl://smtp.gmail.com';
$config['smtp_port']='465';
$config['smtp_timeout']='30';
$config['smtp_user']='yourusername@gmail.com';
$config['smtp_pass']='your password';
$config['charset']='utf-8';
$config['newline']="\r\n";

So, updating the password fixed my problem.

Install CodeIgniter The Secure Way

CodeIgniter is a PHP framework for rapid application development. It’s exceptionally fast and it comes in a small footprint. Installing CodeIgniter is fairly straightforward. You just upload the CodeIgniter files and folders to the directory of your choice.

But, for a much more secure installation, you should probably move the application and system folders above your webroot folder, so that nobody has direct access to it. In addition to moving them, you also need to set the full server paths in the main index.php. You will need to edit index.php found in the main CodeIgniter folder.

Change the following entries to:

$system_path - '/full-server-path/system';
$application_folder = '/full-server-path/application';

Save. That should do it.

CodeIgniter Form_Dropdown

I had a little problem using CodeIgniter’s form_dropdown the other day. Form_dropdown produces an output similar to HTML’s form select option. I was trying to retrieve data queried from a database and display the result in a form. Well, after several test and trials, I finally got the script to work. Here’s how I did it.

Form_Dropdown

The form_dropdown function typically has 3 options. The first option is the fieldname. The second option is the option data usually laid out in an array. The third is the selected data.

form_dropdown(‘name’, array(1?=>’one’,2?=>’two’,3?=>’three’),1);

This produces:

<select name=’name’>
<option value=”1? selected=”selected”>One</option>
<option value=”2?>Two</option>
<option value=”3?>Three</option>
</select>

CodeIgniter

In CodeIgniter, I have a simple script that stores bookmarks. I categorize my bookmarks using tags. When adding bookmarks, I call on the get_dropdown_tags function in Models to retrieve all the tags and display them in array that I can use with form_dropdown. Here are my CodeIgniter entries.

Controller:

$data['tags'] = $this-&gt;links_model-&gt;get_dropdown_tags();

Models:

function get_dropdown_tags() {
  $tags = $this-&gt;db-&gt;query('select distinct tag from links');
  $dropdowns = $tags-&gt;result();
  foreach ($dropdowns as $dropdown) {
    $dropdownlist[$dropdown-&gt;tag] = $dropdown-&gt;tag;
  }
  $finaldropdown = $dropdownlist;
  return $finaldropdown;
}

Views:

In views, I’m simply calling the form_dropdown function using the $tags array passed on by the controller. It’s a pretty neat way to recall data from a database and outputting them in form_dropdown.

<?=form_dropdown(‘tag’,$tags);?>

Finally, a dropdown list that works.

Remove index.php in Codeigniter

If you work with CodeIgniter, you will soon learn fast enough that the CodeIgniter framework, by default, uses the index.php in its URL. Here’s an example of how the URL would look like with the index.php being visible.

CodeIgniter URL

http://example.com/index.php/news/article/my_article

.htaccess

To remove the index.php from the URL, you will need to add mod rewrite rules to the .htaccess file in your main CodeIgniter directory. Use this code. Change the rewrite base to your own settings.

&lt;IfModule mod_rewrite.c&gt;
RewriteEngine On
RewriteBase /ci/
 
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
 
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
 
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
&lt;/IfModule&gt;
 
&lt;IfModule !mod_rewrite.c&gt;
ErrorDocument 404 /index.php
&lt;/IfModule&gt;

config.php

In addition, you will need to edit the /application/config/config.php file. Make sure to remove index.php reference within the file.

$config['index_page'] = '';

Adding FULLTEXT Search

A few days ago, I wrote an article on how to add a search feature in CodeIgniter projects. The method I used was “Pattern Matching” using SQL’s LIKE statement. Although the LIKE statement works great, it is slow. The search will crawl in situations where there are millions of rows in a table.

An alternative method is using a FULLTEXT index search. It uses SQL’s MATCH AGAINST. To start using a FULLTEXT search, a database table will need to be indexed first. You can accomplish this by altering a table and adding FULLTEXT indexing to certain fields:

Alter Table

ALTER TABLE books ADD FULLTEXT(bookname,author,characters,synopsis);

Once a table has been indexed, you can start using MATCH AGAINST.

Match Against

I tried using CodeIgniter’s active record and MATCH AGAINST, but I could not get them to work. So, I ended up writing out the SQL statement using $this->db->query.

$match = $this-&gt;input-&gt;post('search');
$searchcount = $this-&gt;db-&gt;query("SELECT * FROM books WHERE MATCH 
  (bookname,author,characters,synopsis) AGAINST ('.$match.')");

So, if you want to increase search performance, consider using a FULLTEXT search.