CodeIgniter Interview Questions – Set 01

What is the default controller in CodeIgniter?

The file specified in the default controller loaded by default when no file name is mentioned in the URL. By default, it is welcome.php which is the first page to be seen after installing CodeIgniter.

With URL

localhost/codeigniter/
Welcome.php will be loaded as there is no file name mentioned in the URL.

Although as per your need, you can change the default controller in the file application/config/routes.php.

$route[‘default_controller’] = ‘ ‘;
Here, specify your file name which you want to be loaded by default.

How to create a driver in CodeIgniter?

There are three steps to create a driver:

  • Making file structure
  • Making driver list
  • Making driver(s)

Explain the CodeIgniter library. How will you load it?

CodeIgniter provides a rich set of libraries. It is an essential part of CodeIgniter as it increases the developing speed of an application. It is located in the system/library.

It can be loaded as follows,

$this->load->library(‘class_name’);

How can you enable CSRF?

You can enable protection by editing config.php file and setting it to

To enable CSRF make the following statement TRUE from FALSE in application/config/config.php file.

$config[‘csrf_protection’] = TRUE;

Explain model in CodeIgniter.

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

Why is URL routes need to be configured?

There are many purposes for which the URL routes are configured.

  1. To improve the number of page visits.
  2. To hide the code complexities from the user.

What is the basic CodeIgniter URL structure?

Instead of using ‘query-string’ approach, it uses a segment based approach.

Its structure is as follows,

abc.com/class/function/ID
The class represents a controller class that needs to be invoked.

The function is the method that is called.

ID is an additional segment that is passed to controllers.

How to connect multiple databases in CodeIgniter?

To connect more than one database simultaneously, do the following,

$db1 = $this->load->database(‘group_one’, TRUE);
$db1 = $this->load->database(‘group_two’, TRUE);

How can you create a library in CodeIgniter?

There are three methods to create a library,

  • Creating an entirely new library
  • Extending native libraries
  • Replacing native libraries

What is CSRF attack in CodeIgniter?

A CSRF attack forces a logged-on victim’s browser to send a forged HTTP request, including victim’s session cookie and other authentication information, to a web application.

For example, suppose you have a site with a form. An attacker could create a bogus form on his site. This form could contain hidden inputs and malicious data. This form is not sent to the attacker’s site, in fact, it comes to your site. Thinking that the form is genuine, your site process it.

Now suppose that the attacker’s form point towards the deletion form in your site. If a user is logged in and redirected to the attacker’s site and then perform the search, his account will be deleted without knowing him. That is the CSRF attack.