Codeigniter: Apply form validation rules

Validating any form data with user inputs is very important to keep your application safe. Codeigniter provides a native library for form validation named “Form_validation” located at system/libraries/Form_validation.php. Lets see how can we add validation rules to the form created in post(Codeigniter: Create form with form view helper). Step 1: Load form validation library first… Read More »

Codeigniter: Populate form with submitted data for invalid form submission

Codeigniter provides a view helper library (located at system/helpers/form_helper.php) to create forms easily. But, you want to populate form input fields with the entered user data due to issues like invalid form data or some any other error. This can be done using set_value() method of form helper.

Step 1: Load form helper library… Read More »

Codeigniter: Create form with form view helper

Codeigniter provides a view helper library (located at system/helpers/form_helper.php) to create forms easily. Step 1: Load form helper library first in controller or view

Step 2: Use form helper methods to generate form in view file


Codeigniter: Extend native codeigniter classes

To extend any native core class in CodeIgniter (example Input class), you have to create a file named application/core/MY_Input.php and declare your class like below:

NOTE: The prefix “MY_” is configurable and can be renamed to any text as per your requirement by updating the configuration option $config[‘subclass_prefix’] in application/config/config.php file.

This can… Read More »

Codeigniter: Enable CSRF (Cross Site Request Forgery) in CodeIgniter

You can enable CSRF (Cross Site Request Forgery) protection in CodeIgniter by setting below option value to TRUE in application/config/config.php file.

If you will use form helper in order to create forms, then the form_open() function will automatically insert a hidden CSRF field in your forms. Thanks.

Codeigniter: Implement ajax request with json and html response

Implementing ajax request with JSON and HTML response is quite simple as we don’t need to keep in mind to disable layout like in other frameworks. Assuming that the ajax code already exists using your preferred java-script library. Ajax request with JSON response: In controller action that hit with ajax request for JSON response, you… Read More »

Codeigniter: Exception/Error Logging

CodeIgniter provides an easy error handling mechanism to log errors that occur in the application. Step 1: Set option $config[‘log_threshold’] to 1

Step 2: Set $config[‘log_path’] to something else other than the default

Step 3: Set log file extension if default is not needed

Step 4: Set logging date format

Set… Read More »

Codeigniter: Manage sessions

Codeigniter Session Library: Codeigniter provides a core library to manage session located at /system/libraries/Session/Session.php. Initializing a Session: To initialize the Session class manually in your controller constructor, use the $this->load->library() method:

Using session library: Once loaded, the Sessions library object will be available using:

Set session data:

Check session data exists or… Read More »

Codeigniter: Manage flash messages

Reference: Step 1: Create a custom library to manage flash messages

Step 2: Create a partial to render flash message

Step 3: Set flash message

That’s it.

Codeigniter: Get codeigniter object instance using get_instance() method

You can utilize all of Code Igniter’s native resources using $this instance. But $this instance only works within your controllers, your models, or your views. If you would like to use Code Igniter’s native resources from within your own custom classes you can do so as follows:

Once you’ve assigned the object to a variable, you’ll use that… Read More »