Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

4. What are the features of codeigniter.

 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.

Question: How to access config variable in codeigniter? 

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

Question: How to unset session in codeigniter? 

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

Question: How do you get last insert id in codeigniter? 

$this->db->insertid();;

Question: How to print SQL statement in codeigniter model?? 

$this->db->lastquery();;

Question: What are hooks in CodeIgniter?


CodeIgniter's Hooks feature provides a means to tap into and modify the inner workings of the framework without
hacking the core files. 

$hook['pre_controller'] = array(
'class' => 'MyClass',
'function' => 'Myfunction',
'filename' => 'Myclass.php',
'filepath' => 'hooks',
'params' => array('param1', 'param2', 'param3')
);

Question: How to load model in CodeIgniter?

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

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

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’) ;
3) Explain how you will load or add a model in CodeIgniter?

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

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

5) Explain routing in Codeigniter?

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?

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?
CodeIgniter’s Hooks feature provides a way to modify the inner workings or functionality of the framework without
hacking the core files.

The following is a list of available hook points.

 pre_system Called very early during system execution.


 pre_controller Called immediately prior to any of your controllers being called.
 post_controller_constructor Called immediately after your controller is instantiated, but prior to any
method calls happening.
 post_controller Called immediately after your controller is fully executed.
 display_override Overrides the _display() method.
 cache_override Enables you to call your own method instead of the _display_cache() method in the Output
Library. This permits you to use your own cache display mechanism.
 post_system Called after the final rendered page is sent to the browser, at the end of system execution
after the finalized data is sent to the browser.

2) Explain what are hooks in CodeIgniter?

 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.
$config[‘enablehooks’] = TRUE;

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

 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.

SQL prevention use the xss_clean() method as shown below


$data = $this->security->xss_clean($data);

XSS prevention

Escaping Queries : $this->db->escape() function automatically adds single quotes around the data and
determines the data type so that it can escape only string data.

$this->db->escape($email);

    Query Biding

$sql = "SELECT * FROM some_table WHERE id = ? AND status = ? AND author = ?";
$this->db->query($sql, array(3, 'live', 'Rick'));

    Active Record Class

$this->db->get_where('users_table',array
      ('status'=> active','email' => 'mail@ofek.co.in'));
?>

Using active records, query syntax is generated by each database adapter. It also allows safer queries, since the
values escape automatically.

CSRF Prevention 
CSRF stands for cross-site request forgery. You can prevent this attack by enabling it in
the application/config/config.php file as shown below.

$config['csrf_protection'] = TRUE;

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

 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?

 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?
 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?


 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?

 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.
11. How you will use or add codeigniter libraries.
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');
12. How you will work with error handling in codeigniter.
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.

How to get random records in MySQL using CodeIgniter?


order_by function is used to order the records from a table in CodeIgniter.

// Getting random rows from database in CodeIgniter

$this->db->select('*');

$this->db->from('table_name');

$this->db->order_by("column_name", "random");

$result = $this->db->get()->result();

How to get records in MySQL using CodeIgniter wher id = 4?


Where keyword or clause is used to get the records from a table in CodeIgniter.

// Getting record from database in CodeIgniter

$this->db->select('*');

$this->db->from('table_name');

$this->db->where("id_name", $id);
$result = $this->db->get()->result();

How to delete records in MySQL using CodeIgniter


function delete_student_id($id){
$this->db->where('student_id', $id);
$this->db->delete('students');
}

You might also like