Codeigniter Interview Questions and Answers

Q.1.what is CodeIgniter?
A. Codeigniter is an open source framework for web application. It is used to develop websites on PHP. It is loosely based on MVC pattern, and it is easy to use compare to other PHP framework.
Q.2.what are hooks in CodeIgniter?
A. Codeigniter’s hooks feature provides a way to change the inner working of the framework without hacking the core files. In other word, hooks allow you to execute a script with a particular path within the Codeigniter. Usually, it is defined in application/config/hooks.php file.
Q.3.Explain how you will load or add a model in CodeIgniter?
A. Within your controller functions, models will typically be loaded; you will use the function$this->load->model (‘Model_Name’);
Q.4.Explain how you can prevent CodeIgniter from CSRF?
A. There are several ways to protect CodeIgniter from CSRF, one way of doing is to use a hidden field in each form on the website. This hidden field is referred as CSRF token; it is nothing but a random value that alters with each HTTP request sent. As soon as it is inserted in the website forms, it gets saved in the user’s session as well. So, when the form is submitted by the users, the website checks whether it is the same as the one saved in the session. If it is same then, the request is legitimate.
Q.5.Explain how you can extend the class in Codeigniter?
A. To extend the native input class in CodeIgniter, you have to build a file named application/core/MY_Input.php and declare your class withClass MY_Input extends CI_Input {}
Q.6.Mention what is the default URL pattern used in Codeigniter framework?
A. Codeigniter framework URL has four main components in default URL pattern. First we have the server name and next we have the controller class name followed by controller function name and function parameters at the end. Codeigniter can be accessed using the URL helper. For example http://servername/controllerName/controllerFunction/parameter1/parameter2.
Q.7.Explain what is inhibitor in CodeIgniter?
A. For CodeIgniter, inhibitor is an error handler class, using the native PHP functions like set_exception_handler, set_error_handler, register_shutdown_function to handle parse errors, exceptions, and fatal errors.
Q.8.Explain how you can link images/CSS/JavaScript from a view in code igniter?
A. In HTML, there is no Codeigniter way, as such it is a PHP server side framework. Just use an absolute path to your resources to link images/CSS/JavaScript from a view in CodeIgniter/css/styles.css/js/query.php/img/news/566.gpg.
Q.9.Mention what are the security parameter for XSS in CodeIgniter?
A. Codeigniter has got a cross-site scripting hack prevention filter. This filter either runs automatically or you can run it as per item basis, to filter all POST and COOKIE data that come across. The XSS filter will target the commonly used methods to trigger JavaScript or other types of code that attempt to hijack cookies or other malicious activity. If it detects any suspicious thing or anything disallowed is encountered, it will convert the data to character entities.
Q.10.List out different types of hook point in Codeigniter?
A. Different types of hook point in Codeigniter includespost_controller_constructor
Q.12.Why is there a need to configure the URL routes?
A. Why is there a need to configure the URL routes?
Q.13. Explain routing in Codeigniter?
A. In CodeIgniter, the way PHP files served is different rather than accessing it directly from the browser. This process is called routing. Routing in CodeIgniter gives you freedom to customize the default URL pattern to use our own URL pattern according to the requirement. So, whenever there is a request made and matches our URL pattern it will automatically direct to the specified controller and function.
Q.14.what helpers in CodeIgniter are and how you can load a helper file?
A. In CodeIgniter, helpers are group of function in a particular category that assist you to perform specific functions. In CodeIgniter, you will find many helpers like URL helpers- helping in creating links, Text helpers- perform various text formatting routines, Cookies- helpers set and read cookies. You can load helper file by using command $this->load->helper (‘name’) ;
Q.15.Explain how you can enable CSRF (Cross Site Request Forgery) in CodeIgniter?
A. You can activate CSRF (Cross Site Request Forgery) protection in CodeIgniter by operating your application/config/config.php file and setting it to$config [ ‘csrf_protection’] = TRUE;If you avail the form helper, the form_open() function will insert a hidden csrf field in your forms automatically
Q.16.Explain MVC in CodeIgniter.
A. CodeIgniter framework is based on MVC pattern. MVC is a software that gives you a separate logical view from the presentation view. Due to this, a web page contains minimal scripting.Model – The Controller manages models. It represents your data structure. Model classes contain functions through which you can insert, retrieve or update information in your database.
View – View is the information that is presented in front of users. It can be a web page or parts the page like header and footer.
Controllers – Controller is the intermediary between models and view to process HTTP request and generates a web page. All the requests received by the controller are passed on to models and view to process the information.
Q.17.How can you connect models to a database manually?
A. To connect database manually use following syntax,$this->load->database();
Q.18.How can you add or load a model in CodeIgniter?
A. To load models in controller functions, use the following function:$this->load->model(‘ModelName’);If in case your model file is located in sub-directory of the model folder, then you have to mention the full path. For example, if your file location is application/controller/models/project/ModelName. Then, your file will be loaded as shown below :-
Q.19.Explain controller in CodeIgniter.
A. A controller is the intermediary between models and views to process the HTTP request and generates a web page. It is the center of every request on your web application.Consider following URI, n this URI, CodeIgniter try to find Front.php file and Front class.
Q.20. What is the default method name in CodeIgniter?
A. By default controller always calls index method. If you want to call a different method, then write it in the controller?s file and specify its name while calling the function.
Q.21.How can you extend a class in CodeIgniter?
A. You have to build a file name application/core/MY_Input.php and declare your class with Class MY_Input extends CI_Input {}to extend the native input class in CodeIgniter.
Q.22. What is a helper in CodeIgniter? How can a helper file be loaded?
A. Helpers are the group of functions that are used to assist the user to perform specific tasks.URL Helpers: used to create the links.Text Helpers: used for text formatting.Cookies Helpers: used for reading and setting cookies.
Q.23. What are the most prominent features of CodeIgniter?
A. A list of most prominent features of CodeIgniter:

  • It is an open source framework and free to use.
  • It is extremely light weighted.
  • It is based on the Model View Controller (MVC) pattern.
  • It has full featured database classes and support for several platforms.
  • It is extensible. You can easily extend the system by using your libraries, helpers.
  • Excellent documentation.
Q.24. How will you call a constructor in CodeIgniter?
A. To use a constructor, you need to mention the following line of code,
Q.25. Explain the difference between helper and library in CodeIgniter?
A. Helper is a set of Common functions which we can use within Models, Views, Controllers everywhere. Once we include that file then we can get access to the functions.Library is a class which we need to make an instance of the class by $this->load->library() this function.NOTE : A library is used in object-oriented context but a helper is more suitable to be used within the Views
Q.26. What do you mean by Get_instance in CodeIgniter?
A. It is a globally available methods or functions that returns the Controller super-object which contains all the currently loaded classes. get_instance() function returns the Controller class instance.
Q.27. How to remove index.php from URL in Codeigniter?
A. You can follow these given steps to remove index.php from URL in Codeigniter.

  • Please open config.php and change from $config['index_page'] = "index.php" to $config['index_page'] = "";
  • Update also from $config['uri_protocol'] ="AUTO"; to $config['uri_protocol'] = "REQUEST_URI";
  • Put this code in your htaccess file
    RewriteEngine on
    RewriteCond $1 !^(index\.php|resources|robots\.txt)
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [L,QSA]
Q.28. What is Codeigniter Library and how we can load it?
A. Codeigniter provides a rich set of libraries, which indirectly increase the speed of developing an application. These Codeigniter system library is located in system/libraries.In Codeigniter the library can be loaded with $this->load->library('class name');If we want to load multiple libraries then we can use like $this->load->library(array('comment', 'blog'));
Q.29. How to handle sessions in Codeigniter?
A.  How to add values in session in Codeigniter$_SESSION['username'] = '' // It can be use in core PHP
$this->session->set_userdata('username', ''); // It can be done in CodeigniterWe can pass array to store values in session in Codeigniter.$array = array(
'username' => '',
'email' => '',
'url' => ''

How to remove values in session in Codeigniter

unset($_SESSION['username']); // It can be use in core PHP
$this->session->unset_userdata('username'); // It can be done in Codeigniter

If we want to remove more values from session then we can use like this

$this->session->unset_userdata($array);  // $array is defined above. It is an array with key & values.

How to get data from session in Codeigniter

$this->session->userdata('username'); // Here we will get username, if it is exists in session.

Q.31. Explain why codeigniter is called as loosely based mvc framework?
A. It is called as loosely based mvc framework because in codeigniter controller is the necessary but model and view are optional. It means we can build a website without model.
Q.32. How to install CodeIgniter? Explain step by step.
A.  CodeIgniter can be installed with four steps that are given below:

  • Download it from its download page & then unzip the package.
  • Upload its files and folders on your server or localhost.
  • Open application/config/config.php file and set base URL.
  • If you want to use database then open application/config/database.php file and set your database credientails.
  • Now it’s installed
Q.33. What are the server requirements to install codeigniter?
A. We need only PHP version 5.6 or greater than 5.6. because of potential security and performance issues.
Q.32. How we can load multiple helper files in Codelgniter?
A. You have to pass your helpers in array form. Here helper_1, helper_2, helper_3, helper_4 are different helpers that we want to load.
$this->load->helper(array('helper_1', 'helper_2', 'helper_3', 'helper_4'));
Q.33. How we can print SQL statement in codeigniter model?
A. You can use $this->db->last_query(); to display the query string.
You can use print_r($query); to display the query result.
Q.34. Explain how to create custom 404 page in CodeIgniter?
A. You can create your custom 404 page with these steps. Please follow these steps one by by.

  • You have to update your own created controller’s class with $route['404_override'] = 'Controller_Class_Name';. You have to update this in your application/config/routes.php
  • Please create your new controller “Controller_Class_Name” file in your controllers directory application/controllers/.
  • Please put this code in your Controller_Class_Name.phpclass Controller_Class_Name extends CI_Controller {
    public function __construct() {
    }public function index() {
  • Now create your “error_page_404” view file.
Q.35. How to get last inserted id in codeigniter?
A. You can use $lastInsertID = $this->db->insert_id(); after insert query.