CodeIgniter Interview Questions

CodeIgniter Interview Questions | Freshers & Experienced

  • Toby Rogers
  • 27th Dec, 2019
  • 480 Followers

Q1. How to set csrf token in codeigniter?

CSRF stands for Cross-Site Request Forgery. It is an attack that forces an end user to execute unwanted actions on a web application in which they're currently authenticated.

Adding a CSRF token in your forms is a way to prevent your website by CSRF Attack.

CSRF in CodeIgniter:

First, we have to enable CSRF in Config file by adding

$config['csrf_regenerate'] = TRUE;

To add CSRF token in Custom Form

   $csrf = array(
    'name' => $this->security->get_csrf_token_name(),
    'hash' => $this->security->get_csrf_hash()
    );

     <input name="<?=$csrf['name'];?>" value="<?=$csrf['hash'];?>" type="hidden" />

If you are creating your form using CodeIgniter helper then it will be enabled By default.

Q2. What is CodeIgniter?

CodeIgniter is a PHP based open-source software for developing web applications quickly. Released in 2006, CodeIgniter is an MVC based framework where controller classes are necessary, and models & views are not. CodeIgniter also has a lot of libraries for connecting to the database, and performing operations like uploading files, sending emails, managing sessions, etc.

Q3. Enlist major features of CodeIgniter Framework?

Some of the features in CodeIgniter are,

Faster Performance – The loading time of the CodeIgniter application is faster and it offers exceptional performance when compared to other PHP frameworks.

MVC Architecture – It uses the industry-standard MVC architecture for building web applications.

Excellent documentation – The CodeIgniter framework is well documented with good tutorials, books, etc. Any problem that you might encounter in building a CodeIgniter application might already have a solution.

Loosely Coupled – The features in the CodeIgniter works independently without affecting other features. It makes it easy to maintain and upgrade.

Short Learning curve – People who are familiar with the PHP language can easily work with the CodeIgniter framework.

Extendable – You can create your own libraries, packages, helpers if you are not okay with the built-in libraries.

Small footprint – The CodeIgniter source code totally makes up to only 2mb. So it simplifies deploying and updating.

Q4. Why CodeIgniter is called as loosely based MVC framework?

The CodeIgniter follows an MVC based design architecture. But it is called loosely based MVC architecture because the controller in the MVC is the only necessary part. The models and views in the MVC are optional.

Q5. Who is creator of CodeIgniter Framework?

The CodeIgniter is created by EllisLab, and it is now a part of the British Columbia of technology.

Q6. What is ExpressionEngine?

The ExpressionEngine is a Content Management System (CMS) developed by EllisLab. It is a commercial and modular CMS that is suitable for both small static and dynamic sites. It allows the website manager to manage the website without understanding the code.

Some of the features in the ExpressionEngine are managed mailing list, search engine and query tools, user comment system, traffic statistics, variety of plug-ins & modules, and data caching.

Q7. What are hooks in CodeIgniter? List few of them?

Hooks in the CodeIgniter are events that are called before and after the execution of a program. With hooks, you can execute a script in the CodeIgniter execution process without modifying the core files.

For eg: You can check whether a user is logged in or not before executing a controller with hooks.

To enable hook, go to the application/config/config.php file and set the ‘enable_hooks’ config as TRUE.

Different hook points in the CodeIgniter are,

  • PRE_SYSTEM – It is called before the system execution.
  • PRE_CONTROLLER – It is called prior to the controller execution.
  • POST_CONTROLLER – It is called after the controller is executed.
  • DISPLAY_OVERRIDE – It is used to send the final page at the completion of file execution.
  • CACHE_OVERRIDE – It is used to call your custom function in output class.

Q8. What is MVC in CodeIgniter?

CodeIgniter is based on [Model-View-Controller] architecture design. It separates the application logic from the presentation. The Model part in the MVC is used to represent your data structures. With this, you can retrieve, insert, and update data to the database. The View part is used to present the information to the user. In CodeIgniter, the view can be a web page or page fragments like header or footer. The Controller is the interface between the model and the view. It provides the resources needed to process the HTTP request from the client and return the result to generate a web page. MVC in CodeIgniter is called loosely based as the model and view are optional. Only the controller in the CodeIgniter MVC is necessary to define.

Q9. Explain Codeigniter's application Flow Chart?

The CodeIgniter application flowchart works as follows,

  1. When a request comes to the CodeIgniter, it will go to the index.php page first.
  2. The Routing next decides whether to pass the request to the caching or to the security check. If the requested page is in the cache, then the response will go back to the user immediately. If it is not in the caching, then the request will go to the security check.
  3. After the security check is done, the application controller loads all the Models, libraries, helpers, and scripts for the response and passes it to the view.
  4. The view provides the page with available data and passes it on to the caching for future requests.

Q10. How to do 301 redirects in CodeIgniter?

The URL helper function redirect() is used to perform the 301 redirects. The redirect function mainly does a header redirect to the URI specified.

//syntax
redirect($uri=”,$method=’auto’,$code=NULL)

the $uri represents the URI string. $method represents the redirect method such as ‘auto’, ‘location’, or ‘refresh’. $code represents the HTTP response code. This function returns the void type. The third optional parameter is used to create 301 redirects for the purpose of a search engine. The default code is 302, but it can be changed.

//example
redirect(‘/article/11’, ‘location’, 301);

Q11. Explain model in CodeIgniter?

The Models in the PHP CodeIgniter are the PHP classes that work with the information in the database. The model class does the database related operations such as insert, update, and retrieve data from the database. The model classes are mainly stored in the application/models/directory. A basic prototype of the model class is,

class Model_name extent CI_Model{
}

The filename of the model must match the model class name.

Q12. How to load a model in CodeIgniter?

The model class is loaded and called from the controller methods. To load a model class, use the following.

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

If the model is stored in a sub-directory, then include the path.

$this->load->model(‘blog/queries’);

Q13. How can you remove index.php from URL in Codeigniter?

The index.php can be removed from the URL in the CodeIgniter by doing some changes in the .htaccess file if the mod_rewrite is enabled in your apache server. But first, open the config.php file and rewrite the following.

$config[‘index_page’] = “”
$config[‘uri_protocol’] = “REQUEST_URI”
Then, in your .htaccess file replace the last line with RewriteRule ^(.*)$ index.php?/$1 [L, QSA]

Q14. Explain routing in Codeigniter?

In CodeIgniter, routing is responsible for responding to the URL requests. Routing is used to match the URL to the pre-defined routes. If no route is matched, then the Codeigniter throws an exception. You can create your own routing rules in the application/config/routes.php file. Here, in the $route array, you can specify your own routing rules. It can be specified with either wildcards or regular expressions.

Q15. What are helpers in Codeigniter? List some of them?

Helpers in the CodeIgniter are designed to help the developer with specific tasks. It is a collection of functions to do a specific task. Helpers are stored in system/helpers, or application/helpers directory, and you need to load it to use it.

To load a helper, use the following method.

$this->load->helper(‘helper_name’);

Some of the helpers in CodeIgniter are,

  • URL helpers – It helps in creating links.
  • Form helpers – It helps in creating form elements.
  • Text helpers – It helps in-text formatting routines.
  • Cookie helpers – it helps to set and read cookies.
  • File helpers – It helps in dealing with files like reading or writing it.

Q16. What are different types of messages you can log in Codeigniter?

The log messages are written in log files for various purposes using the log_message() function.

There are three different log messages in CodeIgniter such as debug message, error message, and info message.

Error message – These messages are actual errors like PHP error or user error.

Debug message – These messages assist in debugging.

Informational message – It is a low priority message for providing information regarding some processes.

Q17. In which files Codeigniter routes are defined?

The routes in the CodeIgniter are defined in the routes.php file which is stored in the application/config directory.

Q18. What is name of default controller in CodeIgniter?How can you change it.

The default controller parameter in the CodeIgniter is used to load a controller when a URI is not defined. To specify your own default controller, go to the application/config/routes.php file and set the following variable.

$route[‘default_controller’] = “default_controller_name”;

Q19. How to get last inserted id in CodeIgniter?

The insert_id() function is used to return the last inserted id in the CodeIgniter database For eg: $insertId = $this->db->insert_id(); The above line returns the last inserted id in the database ‘db’ to the $insertid variable.

Q20. What are inhibitor in Codeigniter?

The Inhibitor in the CodeIgniter is an error handling class. It uses native PHP functions like set_error_handler, set_exception_handler, etc to handle errors like parse error, exception error, and fatal error.

Q21. How will you call a constructor in CodeIgniter?

To call a constructor in the controller of the CodeIgniter class,

Use the following code in it.

parent::__construct();

Q22. How to load multiple helper files in CodeIgniter?

Helpers in CodeIgniter are used to perform different tasks. Each helper in CodeIgniter has a particular role. To load multiple helpers, specify the helpers in an array.

//example of loading multiple helpers. 
$this->load->helper(array(‘helper1’, ‘helper2’, ‘helper3’, ‘helper4’, ‘helper5'));

Q23. How do you set default timezone in Codeigniter?

The time zone in the CodeIgniter can be set globally in your application’s config file located at application/config.php.

//example 
$config[‘time_reference’] = ‘Asia/Dubai’; 

The above configuration sets the default time zone to Asia/Dubai.

Q24. What is CSRF? How do you set csrf token in codeIgniter?

The CSRF is an attack that forces the victim’s browser to send a forged HTTP request with the victim’s session, cookie, and other information. To protect from this account, CSRF has a secret hidden field called CSRF token. It is a random value that changes for every HTTP request. It makes it tough for hackers to attack.

To enable the CSRF token, go to the application/config/config.php path and set.

$confg[‘csrf_protection’] = TRUE; 

Q25. List out major databases supported by Codeigniter?

The supported database by the CodeIgniter is,

  • MySQL version 5.1 and above through MySQL, MySQL, and PDO drivers.
  • Oracle DB through oci8 and PDO drivers.
  • PostgreSQL through posture and PDO drivers.
  • SQLite through SQLite, sqlite3, and PDO drivers.
  • CUBRID through cubrid and PDO drivers.
  • Interbase and Firebird through ibase and PDO drivers.
  • ODBC through odbc and PDO drivers.

Q26. What is_cli() method do in Codeigniter?

The is_clli() function checks whether the application is running in the command line or not. It returns TRUE if the application is running in the command line and FALSE otherwise.

Q27. How to get random records in MySQL using CodeIgniter?

The order_by() function can be used to order the records in the MySQL database to a random order and get the result in random. Order_by() has two arguments. The first one is the column name, and the second takes ‘RANDOM' to return random value.

//example 
$this->db->order_by(‘id’, ‘RANDOM’); 
$data = $this->db->(“item”);

The first line orders the data into a random order. Second-line gets random data.

Q28. How to enable CodeIgniter hook?

Hooks in the CodeIgniter can be enabled in the config.php file in the following path application/config/config.php file.

$config[‘enable_hooks’] = TRUE; //set TRUE to this parameter to enable the hook. 

Q29. How to initialize or load a driver in CodeIgniter?

Drivers in the CodeIgniter are a special library that has a parent class and many potential child classes.

To use the driver, you need to initialize it within a controller using the following method.

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

Q30. How to check the the installed version of CodeIgniter framework?

To check the version of the CodeIgniter installed in your system, go to the CodeIgniter.php file in the system/core path. Inline 35 of the file, you’ll find the version like define(‘CI_VERSION’, ‘3.0.6’);

Another way is to use the code <?PHP echo CI_VERSION ?>. It returns the version of the CodeIgniter installed.

Q31. What are Sessions In CodeIgniter? How to read, write or remove session in CodeIgniter?

The session class in the CodeIgniter lets you maintain a user's state. It also lets you track user’s activity as they browse on your site.

You can create your session using the Session class in CodeIgniter.

To read data from the session, use the following code.

$this->session->userdata(‘data’); //data is the array index to be retrieved.

To add data to the session, use the following code.

$this->session->set_userdata(‘data’); //data is the array index to be added.

To remove session data, use the following code.

$this->session->unset_userdata(“name”); //it removes all data from the session. 

Q32. How to add or link an images/CSS/JavaScript from a view in CI?

To load CSS, JS, and other resources from a view in the CodeIgniter, first store all the resources in the appropriate folder. CSS file in the CSS folder and JS file in the JS folder.

Then, use the base_url() function to load the resource from view.

To load base_url() method.

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

To use the resource in the view.

<link rel="stylesheet" type="text/css" href="<?php echo base_url(); ?>css/style.css"> 
<script type='text/javascript' src="<?php echo base_url(); ?>js/jquery.min.js"> 
 

Q33. List some common functions of Codeigniter?

Some of the common function in the CodeIgniter is,

Is_php($version)

It takes version number as a parameter. It returns TRUE if the PHP version is at least the version specified and FALSE otherwise.

Is_really_writable($file)

It takes a file path as a parameter. It returns TRUE if you can’t write to the file and FALSE otherwise.

Is_https()

It returns TRUE if you are currently using HTTP-over SSL, and FALSE otherwise.

Is_cli()

It returns TRUE if you are running in the command line, and FALSE otherwise.

Get_mimes()

It returns a reference to the MIME array from the mimes.php file in the application/config path.

Q34. How to set or get config variables in Codeigniter?

We can set or get config variables in Codeigniter with the writes of these codes.

To get/retrieve an item from the config file, use the following line.

$this->config->item(‘item_name’); 

To set an item or change the value of the existing item, use the following code.

$this->config->set_item(‘item_name’, ‘item_value’); 

Q35. How to delete a record in Codeigniter?

The record can be deleted using the delete() function. First, get the id of the record to delete that particular record.

//code to delete a record in CodeIgniter. 
$this->db->delete(‘table’, array(‘id’ => $id)); //it deletes the $id record from the table. 

Q36. List the resources that can be autoloaded in Codeigniter?

The autoload feature in the CodeIgniter lets you initialize libraries, helpers, and models automatically for every time the system runs.

Things that can be loaded automatically are,

  • Helper files in the helper directory.
  • Custom config files in the config directory.
  • Language files in the system/language directory.
  • Models in the model folder.
  • Classes present in the library directory.

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

CodeIgniter uses a segment-based approach than a query-based approach for URL. The main components of the URL are controller, server name, action or method, and parameter.

//Example URL in the CodeIgniter.

www.exampel.com/user/get/name

Here example.com is the server name. The user is the controller. Get in the action or method. The name is the parameter.

Q38. What is an ORM, List ORM's for CodeIgniter?

An ORM (Object Relational Mapper) is designed to map the database tables in your application to easily work with objects. It also creates a relationship between them.

Some of the ORM used in CodeIgniter are.

Gas ORM – An ORM built for the CodeIgniter app that uses database packages to support numerous DB drivers. A lightweight and easy-to-use ORM that provides a method to map the database into objects.

Doctrine Mapper – It has several PHP libraries to focus on database storage and object mapping. It is built upon ORM and DABL (Database Abstraction Layer).

DataMapper – An ORM that is written in PHP to map the database into objects.

Q39. In which directory logs are saved in Codeigniter?

The log messages in the CodeIgniter are stored in /application/logs folder. To store the log, make the logs folder writable.

Q40. How to enable error logging in Codeigniter?

To enable the error logging in CodeIgniter, set $config[‘log_threshold’] variable as 1 in the config.php file present at /application/config path.

Q41. How to connect multiple databases in CodeIgniter?

To connect to multiple databases simultaneously in the CodeIgniter,

Use the following code.

$DB1 = $this->load->database('group_one', TRUE); 
$DB2 = $this->load->database('group_two', TRUE); 

Here group_one and group_two are the specific database group specified in your config file.

About Author :

  • Author of CodeIgniter Interview Questions

    Toby Rogers

    Toby Rogers is blogger, Developer and Data Science Expert. He have good understanding of nosql databases, Python, Lisp and Scala Programming Language.

Leave A Comment :

Valid name is required.

Valid name is required.

Valid email id is required.