Top 15 CodeIgniter Interview Questions and Answers for 2024

Industry:, ,

1. How to find the version of the Codeigniter framework we are currently using?

Finding out the version can be done in two ways.

The first way is to run the below code:<?php echo CI_VERSION;?>

The second way is to obtain the system/core/CodeIgniter.php directory and run the code given below:

define('CI_VERSION', '2.1.4');

2. What is the architecture of Codeigniter?

The architecture of Codeigniter has three essential points:

  • It is dynamically instantiated, making it incredibly lightweight.
  • The components in Codeigniter are independent, making it loosely coupled.
  • Every component has its singularity, as every class and function focuses on its purpose.

3. What are the various functions used in Codeigniter?

The functions in Codeigniter are globally defined and freely available. Loading of libraries or helpers is not required when using these functions.is_really_writable($file) is_php($version) html_escape($var) config_item($key) set_status_header($code[, $text = ”]) is_https() remove_invisible_characters($str[, $url_encoded = TRUE]) is_cli() get_mimes()

4. How is the default timezone set in Codeigniter?

The default timezone in Codeigniter is set by opening the application using the config.php file and adding this code to it.

date_default_timezone_set('your timezone');

5. What is the procedure to add/link JavaScript/Images?CSS in Codeigniter?

To link or add the images/CSS/JavaScript in Codeigniter absolute path to your resources is used.

For example// Refers to $config['base_url'] <img src="<?php echo site_url('images/myimage.jpg'); ?>" />

6. What are the sessions in Codeigniter?

Sessions in CodeIgniter are the classes that help maintain a user’s “state” and track their activity when they browse the website.

To use the session, the Session class is to be loaded in the controller.

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

sessions in Codeigniter use this method.

$this->load->library('session');

The Sessions library object will be available to use once the files are loaded:

$this->session

7. How to extract the last inserted id in Codeigniter?

The DB Class insert_id() method is used in Codeigniter to get the last insert id.

Usage:

function add_post($post_data){
$this->db->insert('posts', $post_data);
$insert_id = $this->db->insert_id();
return $insert_id;
}

8. How to check the existence of a field or a column in any table in Codeigniter?

This code is used to check the presence of a field or a column in any table.

if ($this->db->field_exists('field_name', 'table_name'))
{
// some code...
}

9. What are the commonly used URL helpers in Codeigniter?

URL helpers use a segment-based approach instead of the ‘query-string’ approach.

The URL structure is as follows,

abc.com/class/function/ID

  • Class represents the controller class that is to be invoked.
  • A function is a method that is called.
  • ID is an additional segment that is passed to controllers.

10. How is the csrf token set in Codeigniter?

Csrf sets the protection in Codeigniter. To set csrf, the config value must be set to True.

Syntax: $config['csrf_protection'] = TRUE;

11. How and in which directory are the logs saved in Codeigniter?

All the logs in Codeigniter are by default stored in logs/ directory. To enable error logging, the “threshold” must be set for logging in application/config/config.php. The logs must be writable.

$config['log_threshold'] = 1;

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

Different types of hook point in Codeigniter includes

  • post_controller_constructor
  • pre_controller
  • post_system
  • pre_system
  • cache_override
  • display_override
  • post_controller

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

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.

These interview questions will also help in your viva(orals)

14. How to do 301 redirects in CodeIgniter?

We can use redirect helper to do 301 redirects in Codeigniter.

Syntax : redirect($uri = ”, $method = ‘auto’, $code = NULL)

Parameter: $uri (string) – URI string $method (string) – Redirect method (‘auto’, ‘location’ or ‘refresh’) $code (string) – HTTP Response code (usually 302 or 303)

Return type: void

Sample Usage:-redirect(‘/posts/13’, ‘New location’, 301);

15. How to check a field or column exists in a table or not in Codeigniter?

Code for Checking a field or column exists or not in a Codeigniter table.if ($this->db->field_exists(‘field_name’, ‘table_name’)) { // some code… }

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

In Codeigniter by default all config variables are located at “application/config/config.php” file.
Below is the way to set or get a config variable in Codeigniter

// Setting a config variable dynamically$this->config->set_item(‘variable_name’, value); // Getting value of config variable in Codeigniter. $this->config->item(‘variable_name’);

17. How to delete a record in Codeigniter?

In Codeigniter, delete function is used to delete the one or more row data from a table.

//DELETE FROM table WHERE id = $id$conditions =[‘id’ => $id] $this->db->delete(‘table_name’, $conditions); // Deleting records from more than one tables in one go $id=1; $tables = array(‘table1’, ‘table2’, ‘table3’); $this->db->where(‘id’, $id); $this->db->delete($tables)

case studies

See More Case Studies

Contact us

Partner with Us for Comprehensive IT

We’re happy to answer any questions you may have and help you determine which of our services best fit your needs.

Your benefits:
What happens next?
1

We Schedule a call at your convenience 

2

We do a discovery and consulting meting 

3

We prepare a proposal 

Schedule a Free Consultation