Most frequently asked Codeigniter Interview Questions and Answers specially for freshers and experienced PHP developer.


 

1. Explain what is CodeIgniter?

Ans:

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.

2. Explain what are hooks in CodeIgniter?

Ans:

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.

3. Explain how you will load or add a model in CodeIgniter?

Ans:

Within your controller functions, models will typically be loaded; you will use the function

$this->load->model (‘Model_Name’);

4. Explain what helpers in CodeIgniter are and how you can load a helper file?

Ans:

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’) ;

5. Explain routing in Codeigniter?

Ans:

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.

6. Why is there a need to configure the URL routes?

Ans:

Changing the URL routes has some benefits like
From SEO point of view, to make URL SEO friendly and get more user visits
Hide some URL element such as a function name, controller name, etc. from the users for security reasons
Provide different functionality to particular parts of a system

7. List out different types of hook point in Codeigniter?

Ans:

Different types of hook point in Codeigniter includes

  1. post_controller_constructor
  2. pre_controller
  3. post_sytem
  4. pre_system
  5. cache_override
  6. display_override
  7. post_controller

8. Mention what are the security parameter for XSS in CodeIgniter?

Ans:

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.

9. Explain how you can link images/CSS/JavaScript from a view in code igniter?

Ans:

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

10. Explain what is inhibitor in CodeIgniter?

Ans:

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.



 

11. Mention what is the default URL pattern used in Codeigniter framework?

Ans:

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.

12. Explain how you can extend the class in Codeigniter?

Ans:

To extend the native input class in CodeIgniter, you have to build a file named application/core/MY_Input.php and declare your class with

Class MY_Input extends CI_Input {
}

13. Explain how you can prevent CodeIgniter from CSRF?

Ans:

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.

14. Explain how you can enable CSRF (Cross Site Request Forgery) in CodeIgniter?

Ans:

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

15. What is CodeIgniter ?

Ans:

Codeigniter is the most popular open source PHP framework. It is very simple and lightweight. It can be used to develop any kind of web project from small website to large scale of application.

16. Why use Hooks in CodeIgniter

Ans:

Hooks are the special feature of CodeIgniter that allows to change the inner functionality of framework without making any change in core files of framework. The Hooks are defined in application/config/hooks.php.

17. What is routing in CodeIgniter ?

Ans:

Instead of accessing files directly from browser, Routing enables to serve files differently. Routing is an important part of CodeIgniter that enables to customize default URL pattern according to requirement to use your own and it redirect automatically to matched URL pattern by specified controller and function.

18. How to load Model in CodeIgniter?

Ans:

Models are PHP classes that are designed to work with information in your database. In CodeIgniter, Model classes are stored in your application/models/ folder. The models will typically be loaded and called from within your controller functions. To load a model you will use the following function:

$this->load->model('Model_name');

19. What are the Helpers in CodeIgniter?

Ans:

The Helpers are the group of functions in a specific category that helps to perform particular functions. There are many helpers in CodeIgniter that you can use according to your requirement. The major CodeIgniter helpers are URL Helpers that assist in creating link, Form Helpers that help you create form elements, Text Helpers to perform various text formatting routines, Cookie Helpers to set and read cookies, File Helpers to help you deal with files etc.

20. How to unset session in CodeIgniter?

Ans:

We can unset session variable in CodeIgniter like below.

$this->session->unsetuserdata('seesion_variable');



 

21. How to access config variable in CodeIgniter?

Ans:

We can access config variable name in CodeIgniter like below.

$this->config->item('variable name');

22. How to use or add libraries CodeIgniter?

Ans:

To use new libraries in your project, we can add the new library to the autoload library array in config/autoload.php.

$autoload['libraries'] = array ('database', 'session', 'auth');

23. What is inhibitor in CodeIgniter?

Ans:

The Inhibitor is an error handler class in CodeIgniter. With Inhibitor, we can handle parse errors, exceptions, fatal errors while using native PHP functions like et_exception_handler, set_error_handler, register_shutdown_function tec.

24. Why CodeIgniter called as loosely based mvc framework?

Ans:

Because CodeIgniter does not need to follow strict MVC patter while creating applications. It’s not essential to create model, we can create application with view and controllers only.

25. In Which language CodeIgniter is written?

Ans:

In PHP

26. Explain routing in CodeIgniter?

Ans:

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.

27. What are the features of codeigniter?

Ans:

  • It is free to use as for open source framework.
  • CodeIgniter is Fast. It is faster than any other framework in php.
  • It is extremely light weighted.
  • Excellent documentation
  • CodeIgniter Uses MVC (Model View Controller) which allows great separation between logic and presentation.

28. How to access config variable in codeigniter?

Ans:

$this->config->item('variableName');

29. Explain how you can enable CSRF (Cross Site Request Forgery) in CodeIgniter?

Ans:

Go To: application/config/config.php and set

$config['csrf_protection'] = TRUE;

30. What is Model in CodeIgniter?

Ans:

Model’s responsibility is to handle all data logic and representation and load data in the views. It is stored in application/models folder.


 

31. How to load a model in CodeIgniter?

Ans:

$this->load->model('ModelName');

32. How to unset session in Codeigniter?

Ans:

$this->session->unsetuserdata('SessionVariableName');

33. What is codeigniter.

Ans:

Codeigniter is open source , web application framework.Its is for building websites using php.Codeigniter is loosely based on MVC pattern. Most simple framework in php , which is you will easily learn.Its mostly known for its speed as compare to other frameworks in php.
Its goal is to enable you to develop projects much faster than you could if you were writing code from scratch, by providing a rich set of libraries for commonly needed tasks, as well as a simple interface and logical structure to access these libraries.

34. When and who developed codeigniter.

Ans:

The first public version of CodeIgniter was released on February 28, 2006.

35. What is current version of codeigniter.

Ans:

version 2.2.0 was released June 2014.The development version of codeigniter 3.0-dev was release on October 20, 2011.Preview version 3.0-dev, is certified open source software licensed with the Open Software License.

36. What are the features of codeigniter.

Ans:

  • Codeigniter is free to use,its an open source framework.
  • Its light weight.The core system requires only a few very small libraries.Not like other frameworks that require heavy file libraries.
  • CodeIgniter is Fast.Its faster than any other framework in php.
  • The URLs generated by CodeIgniter are clean and search-engine friendly.You will change any url to what ever you want from files.
  • CodeIgniter is Extensible.The system can be easily extended through the use of your own libraries, helpers, or through class extensions or system hooks.
  • CodeIgniter Uses MVC(Model View Controller) which allows great separation between logic and presentation.
  • CodeIgniter requires nearly zero configuration,does not require you to use the command line,not forced to learn a templating language.
  • Full Featured database classes with support for several platforms,Security and XSS Filtering,Error Logging.

37. Explain Codeigniter File Structure.

Ans:

When you download Codeigniter you will see the following folder structure :-

  • application
    • cache
    • Config
    • Controllers
    • core
    • errors
    • helpers
    • hooks
    • language
    • libraries
    • logs
    • models
    • thirdparty
    • views
  • system
    • core
    • database
    • fonts
    • helpers
    • language
    • libraries

38. Explain Application Flow Chart in codeigniter.

Ans:

Application Flow Chart in codeigniter

Application flow chart from Codeigniter documentaion :

  • The index.php serves as the front controller, initializing the base resources needed to run CodeIgniter.
  • The Router examines the HTTP request to determine what should be done with it.
  • If a cache file exists, it is sent directly to the browser, bypassing the normal system execution.
  • Security. Before the application controller is loaded, the HTTP request and any user submitted data is filtered for security.
  • The Controller loads the model, core libraries, helpers, and any other resources needed to process the specific request.
  • The finalized View is rendered then sent to the web browser to be seen. If caching is enabled, the view is cached first so that on subsequent requests it can be served.

39. Explain MVC in Codeigniter.

Ans:

Model–View–Controller (MVC) is an architecture that separates the representation of information from the user’s interaction with it.

MVC in Codeigniter

Controller:- The Controller serves as an intermediary between the Model, the View. controller mediates input, converting it to commands for the model or view.

Model:-The Model represents your data structures. Typically your model classes will contain functions that help you retrieve, insert, and update information in your database.The model consists of application data and business rules.

View:-The View is the information that is being presented to a user. A View will normally be a web page.A view can be any output representation of data.

For more detail understanding MVC please read this article What is MVC(Model-View-Controller) Architecture.

40. What are the hooks in codeigniter.

Ans:

CodeIgniter’s Hooks feature provides a means to tap into and modify the inner workings of the framework without hacking the core files.How ever you like to cause some action to take place at a particular stage in the execution process. In other word, hooks allow you to execute a script with a particular path within the Codeigniter.

The hooks feature can be globally enabled/disabled by setting the following item in the application/config/config.phpfile:

$config['enable_hooks'] = TRUE;

Hooks are defined in application/config/hooks.php file.For example

$hook[‘pre_controller’] = array(
‘class’ => ‘MyClass’,
‘function’ => ‘Myfunction’,
‘filename’ => ‘Myclass.php’,
‘filepath’ => ‘hooks’,
‘params’ => array(‘test’, ‘test1’, ‘webs’)
);

In the above example hook ‘pre_controller‘ is called hook point. There are many different types of hook point in Codeigniter which is below-

  • post_controller_constructor
  • pre_controller
  • post_sytem
  • pre_system
  • cache_override
  • display_override
  • post_controller


 

41. How you will add or load an model in codeigniter.

Ans:

Models will typically be loaded and called from within your controller functions. To load a model you will use the following function:

$this->load->model('Model_name');

42. What are the helpers in codeigniter.

Ans:

Helpers, as the name suggests, help you with tasks. Each helper file is simply a collection of functions in a particular category.There are URL Helpers, that assist in creating links, there are Form Helpers that help you create form elements, Text Helpers perform various text formatting routines, Cookie Helpers set and read cookies, File Helpers help you deal with files, etc.
Loading a helper file is quite simple using the following function:

$this->load->helper('name');

43. How you will use or add codeigniter libraries.

Ans:

All of the available libraries are located in your system/libraries folder. In most cases, to use one of these classes involves initializing it within a controller using the following initialization function:-

$this->load->library('class name');

44. How you will work with error handling in codeigniter.

Ans:

CodeIgniter lets you build error reporting into your applications using the functions:-

  • show_error():- This function will display the error message supplied to it using template application/errors/error_general.php.
  • show_404() :- Function will display the 404 error message.
  • log_message(‘level’, ‘message’) :- This function lets you write messages to your log files. You must supply one of three “levels” in the first parameter, indicating what type of message it is (debug, error, info), with the message itself in the second parameter.

45. What are the security parameter for XSS in CodeIgniter?

Ans:

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.

46. Explain how you can enable CSRF (Cross Site Request Forgery) in CodeIgniter?

Ans:

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.

47. Explain how you can prevent CodeIgniter from CSRF?

Ans:

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.

48. Explain what is inhibitor in CodeIgniter?

Ans:

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.

49. Explain routing in Codeigniter?

Ans:

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.

50. Why is there a need to configure the URL routes?

Ans:

Changing the URL routes has some benefits like

  • From SEO point of view, to make URL SEO friendly and get more user visits
  • Hide some URL element such as a function name, controller name, etc. from the users for security reasons
  • Provide different functionality to particular parts of a system.



 

51. Explain what is CodeIgniter?

Ans:

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.

52. What are the features of codeigniter?

Ans:

  • Codeigniter is free to use,its an open source framework.
  • Model-View-Controller Based System.
  • Extremely Light Weight.
  • Full Featured database classes with support for several platforms..
  • CodeIgniter is Extensible.The system can be easily extended through the use of your own libraries, helpers, or through class extensions or system hooks.
  • Excelent documentation.
  • Full Featured database classes with support for several platforms,Security and XSS Filtering,Error Logging.

53. List out different types of hook point in Codeigniter?

Ans:

Different types of hook point in Codeigniter includes

  • post_controller_constructor
  • pre_controller
  • post_sytem
  • pre_system
  • cache_override
  • display_override
  • post_controller

54. Explain Codeigniter File Structure?

Ans:

following are the folder structure :-

  • application
    • cache
    • Config
    • Controllers
    • core
    • errors
    • helpers
    • hooks
    • language
    • libraries
    • logs
    • models
    • thirdparty
    • views
  • system
    • core
    • database
    • fonts
    • helpers
    • language
    • libraries

55. Explain what are hooks in CodeIgniter?

Ans:

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.

56. Explain MVC in Codeigniter?

Ans:

CodeIgniter is based on the Model-View-Controller development pattern. MVC is a software approach that separates application logic from presentation. In practice, it permits your web pages to contain minimal scripting since the presentation is separate from the PHP scripting.

  • The Model represents your data structures. Typically your model classes will contain functions that help you retrieve, insert, and update information in your database.
  • The View is the information that is being presented to a user.
  • A View will normally be a web page, but in CodeIgniter, a view can also be a page fragment like a header or footer. It can also be an RSS page, or any other type of “page”.
  • The Controller serves as an intermediary between the Model, the View, and any other resources needed to process the HTTP request and generate a web page.

57. Explain how you can prevent CodeIgniter from CSRF?

Ans:

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.

58. How you will load an model in codeigniter?

Ans:

$this->load->model('Modelname');

59. Explain what helpers in CodeIgniter are and how you can load a helper file?

Ans:

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’) ;

60. What are the security parameter for XSS in CodeIgniter?

Ans:

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.


 

61. Explain what is inhibitor in CodeIgniter?

Ans:

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.

62. What are the hooks in codeigniter?

Ans:

CodeIgniter’s Hooks feature provides a means to tap into and modify the inner workings of the framework without hacking the core files. When CodeIgniter runs it follows a specific execution process, diagramed in the Application Flow page. There may be instances, however, where you’d like to cause some action to take place at a particular stage in the execution process. For example, you might want to run a script right before your controllers get loaded, or right after, or you might want to trigger one of your own scripts in some other location.
The hooks feature can be globally enabled/disabled by setting the following item in the application/config/config.php file:

$config['enablehooks'] = TRUE;

63. Explain routing in Codeigniter?

Ans:

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.

64. What is the default URL pattern used in Codeigniter framework?

Ans:

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.

65. Why is there a need to configure the URL routes?

Ans:

Changing the URL routes has some benefits like
From SEO point of view, to make URL SEO friendly and get more user visits
Hide some URL element such as a function name, controller name, etc. from the users for security reasons
Provide different functionality to particular parts of a system

66. logmessage(‘level’, ‘message’)

Ans:

This function lets you write messages to your log files. You must supply one of three “levels” in the first parameter, indicating what type of message it is (debug, error, info), with the message itself in the second parameter.

67. How do you set default timezone in codeigniter?

Ans:

We can do by adding datedefaulttimezoneset(‘America/Los_Angeles’); in index.php

68. How you will work with error handling in codeigniter?

Ans:

CodeIgniter lets you build error reporting into your applications using the functions described below. In addition, it has an error logging class that permits error and debugging messages to be saved as text files.
showerror(‘message’ [, int $statuscode= 500 ] )
This function will display the error message supplied to it using template application/errors/errorgeneral.php.
show404(‘page’ [, ‘logerror’])
This function will display the 404 error message supplied to it using template application/errors/error404.php.
logmessage(‘level’, ‘message’)
This function lets you write messages to your log files. You must supply one of three “levels” in the first parameter, indicating what type of message it is (debug, error, info), with the message itself in the second parameter.

69. How do you use aliases with autoloading models in codeigniter?

Ans:

We can auto load model like this:

$autoload[‘model’] = array(array(‘usersmodel’, ‘users’), array(‘newsmodel’, ‘news’), ‘categorymodel’);