7 Codeigniter Tips to Improve Your Programming

7 Codeigniter Tips to Improve Your Programming

CodeIgniter is a widely used PHP framework that aims to assist developers write better structured code and take away boilerplate tasks from the workflow. Below are some Codeigniter Tips to improve your programming skills, Please take a look.

1. CodeIgniter Tips in Error Handling

Each time a page is run, a log file is created. These log files are under designer’s control. This function permits you to write messages in the log file.

Syntax :

log_message(‘level’,‘message’);

First Parameter level shows which kind of error message you need to log. There are 3 levels of error messages : debug, error and info.

Example :

log_message(‘error’,‘username is empty’);
  • Error Messages : These are actual errors, such as PHP errors or user errors.
  • Debug Messages : These are the messages that help in debugging.
  • Information Messages : These are the messages that give information about some procedure. These are low priority messages. CodeIgniter doesn’t produce these messages on its own.
  • When an error occurs, it is made accessible for logging, if the logging threshold permits it. That implies logging of errors is controlled by log_threshold configuration. The logging threshold is available in application/config/config.phpas.
$config[‘log_threshold’]=0;

The value zero disables logging i.e. error logging is turned off.

2. Get Browser Information in CodeIgniter

The User Agent Class provides functions that recognize information about the browser, mobile device. Likewise you can get referrer data as well as language and supported character-set data.

The user agent name definitions are located in a config file located at: application/config/user_agents.php.

Get Browser Name, Version and OS Platform using following PHP Code.

$this->load->library(‘user_agent’);
echo " You are using ". $this->agent->browser(). " Version# ". $this->agent->version();
echo $this->agent->platform();

3. Remove ‘index.php’ in the URLs using htaccess

By default CodeIgniter includes the index.php file in your URLs. To make friendlier and web crawler improved URL, remove the ‘index.php’ from your URLs using htaccess. Include the below code in an htaccess file & upload to your server and it will remove the ‘index.php’ from URL.

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

4.  Loading up multiple databases in CodeIgniter

Connecting to multiple databases is Simpler in CodeIgniter. You should simply to set up a separate connection for each database you wish to work. Here You’ll see the Codeigniter Tips  to connect with two MYSQL Databases

  •  Open the “application/config/database.php” file.
  • Now You can see a list of default connection settings provided. Enter hostname, username, password, database.
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'mysql_username';
$db['default']['password'] = 'mysql_password';
$db['default']['database'] = 'employee';
$db['default']['dbdriver'] = 'mysql';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;
  • Make Another Database Connection with different Name and provide the details
$db['ADMINDB']['hostname'] = 'localhost';
$db['ADMINDB']['username'] = 'mysql_username';
$db['ADMINDB']['password'] = 'mysql_password';
$db['ADMINDB']['database'] = 'admin';
$db['ADMINDB']['dbdriver'] = 'mysql';
$db['ADMINDB']['dbprefix'] = '';
$db['ADMINDB']['pconnect'] = TRUE;
$db['ADMINDB']['db_debug'] = TRUE;
$db['ADMINDB']['cache_on'] = FALSE;
$db['ADMINDB']['cachedir'] = '';
$db['ADMINDB']['char_set'] = 'utf8';
$db['ADMINDB']['dbcollat'] = 'utf8_general_ci';
$db['ADMINDB']['swap_pre'] = '';
$db['ADMINDB']['autoinit'] = TRUE;
$db['ADMINDB']['stricton'] = FALSE;
  • Now You can access the database like this :
//Default Database

$this->load->database();
$query = $this->db->get('staff');
foreach ($query->result() as $row)
     echo $row->name;

//Second Database

$admin_db= $this->load->database('ADMINDB', TRUE);
$query = $admin_db->get('members');
foreach ($query->result() as $row)
     echo $row->role;

5. Filter all user inputs before adding them to Database

If your application heavily depends on user input, then set $config[‘global_xss_filtering’] = TRUE in config.php file. Don’t forget to filter & escape user input for better security.

6. Leveraging HTML Emails in CodeIgniter

Need to send HTML emails through your website is  ubiquitous, it be a newsletter or some auto produced welcome message. Generally we write our hard coded corresponding html in the message parameter of our email object.

Well, it’s a fact that the above method is pretty ugly.

Being a developer I personally prefer to use this approach to send a newsletter, and several other HTML emails, and using templates will significantly ease up the process.

In your application’s views folder, create a folder called emails and place any HTML email you’d like to send inside it, with the corresponding structure to be rendered by email clients. And now, in the method where you shoot off the emails, you can use the following code:

public function send_mail() {

$this->load->library( 'email' );
$this->email->from( 'sender@gmail.com', 'Sender Name' );
$this->email->to( 'reciever@gmail.com' );
$this->email->subject( 'Subject of the email' );
$this->email->message( $this->load->view( 'emails/message', $data, true ) );
$this->email->send();

}

7. Protect your site from Cross-Site Request Forgery (CSRF)

To protect the site from CSRF attacks always enable the CodeIgniter settings for CSRF protection. To enable it, open your config file and look for the code written below:

$config['csrf_protection'] = TRUE;

Submit your CodeIgniter Errors, issues or problems and get it fixed by a CodeIgniter Expert.