×

CodeIgniter Security Helper

A Security helper file contains some predefined functions that are used to protect application from unauthorized access.

Loading the Helper

The following syntax is used to load the security helper in the CodeIgniter application.

Syntax

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

The Function of Security helper

  1.  xss_clean() : xss_clean means Cross-site scripting, which is used to provide security in web application. It is a technique to stop and filter the JavaScript or malicious code and convert it into safe character entities.

Syntax

xss_clean ($str [, $is_image = FALSE]);

      It has two parameters:

      $str : It takes an input string for passing in xss_clean() function.

      $is_image : It shows whether you are dealing with image or not. If no, pass FALSE                            otherwise TRUE.

      Example:

      echo "<b> xss_clean() function : </b>". xss_clean($disp, $is_image =FALSE). 

2.sanitize_filename(): This function is used to provide security to levels of directory such as application/folder/files by converting them into a plain text.

Syntax

sanitize_filename ($filename);

$filename : It takes a filename as a string.

Example

echo
sanitize_filename ('C:/wamp64/www/CodeIgniter-3.1.11/);  /* it shows directory level in a
simple text such as C:wamp64wwwCodeIgniter-3.1.11 */

3.do_hash(): It is a hashing technique that converts your text to a secure encrypted format using the md5 and sha1 algorithm.

Syntax

do_hash ( $str [, $type = ‘sha1’ ]);

It has two parameters:

  • $str : It takes an input string.
  • $type : It defines which type of algorithm you want to implement in a hash function.

Example

$info="programmer";
 echo do_hash($info, 'md5'); // It encrypt word ‘programmer’ using md5 algorithm 

4.strip_image_tags(): It is a security function that removes the image tags from a string and converts the image URLs into a  plain text.

Syntax

strip_image_tags ($str);

$str: It defines an image file as an input string.

Example:

   $data = '<img src = "CodeIgniter-3.1.11/images/cloud.jpg"></img>';
 echo "<b> strip_image_tags() function : </b>".strip_image_tags($data); /* it shows image path as a simple text */ 

5.encode_php_tags(): This function is used to remove the php tag from the input string and convert it into a secure entity.

Syntax

encode_php_tags ($str);

$str: It holds an input strings that pass on the encode_php_tags () function.

Example:

echo encode_php_tags( “Hello Preogrammer” );

Create a simple program of Security helper

Create a controller file Online.php and save it in application/controller. After that, write the following program in your controller file.

<?php
 class Online extends CI_Controller
 {
     public function secure()
     {
         $this->load->helper('security');
          echo “<title> Tutorial and Example </title>”;
             // xss_clean() function
         $var = "<script> alert ('hello hacker') </script>";
         $disp = "<script> It is used to filter out the potential xss attacks. </script>";
         echo "<b> xss_clean() function : </b>". xss_clean($disp, $is_image =FALSE). "<br>";
        echo xss_clean($var);
        echo "<br>";
             // sanitize_filename() function
        echo " <b> sanitize_filename() function : </b>".sanitize_filename ('C:/wamp64/www/CodeIgniter-3.1.11/Files/hello');
        echo "<br>";
        // do_hash() function with SHA1 Algorithm
        $data ='welcome';
        echo "<b>welcome message convert into SHA1 Security </b> :".do_hash($data); // SHA1
        echo "<br>";
             // do_hash() function with MD5 algorithm
        $show ="Codeigniter";
        echo "<b> CodeIgniter message convert into md5 security </b> :".do_hash($show, 'md5');
    //  strip_image_tags() function
    $data = '<img src = "CodeIgniter-3.1.11/images/valley.jpg"></img>';
        echo "<b> strip_image_tags() function : </b>".strip_image_tags($data);
        echo "<br>";
        // encode_php_tags() function
         $va = "Welcome to the CodeIgniter tutorials";
        echo "<b> encode_php_tags() function : </b>".encode_php_tags($va);   
       echo "<br>";
 }
     }
 ?> 

When you execute the above program in localhost by invoking the URL localhost/CodeIgniter-3.1.11/index.php/online/, itcalls the secure functionandshows the output, as shown below.

CodeIgniter Security Helper

Related Topics

CodeIgniter Text Helper

The text helper contains the various function that is used to operate with text such as define a word limit and characters limit, conversion of word from ASCII to entities, etc. Loading the text...

4 minutes read.

CodeIgniter User Agent Class

User Agent Class The CodeIgniter provides a user agent class that helps to identify and collect information about the browser, mobile devices, or robots visiting your site. Besides this, it is also used...

4 minutes read.

Form Validation Library Codeigniter

Form Validation Library The Codeigniter provides a form validation library that contains predefined rules to validate the form fields. It also helps in optimizing the form validation code, which reduces the effort and time...

31 minutes read.

CodeIgniter Tutorial

What is CodeIgniter? CodeIgniter is an open-source framework for developing a dynamic web application with the use of PHP. It uses the MVC framework to develop the project, which is much...

3 minutes read.

CodeIgniter String Helper

The string helper file contains functions that allows separate operations with strings in CodeIgniter. Loading the String Helper Before using the string helper in the CodeIgniter application, you must load it in the controller...

7 minutes read.

CodeIgniter Inflector Helper

The inflector helper file contains some predefined function that allows users to change their English words into plural, singular, and camel case, etc. Loading an Inflector Helper Before using an inflector helper function, you must...

3 minutes read.

Installation of CodeIgniter

Before installing CodeIgniter, you need to make sure that the wamp or xampp server in the system is preinstalled. The installation procedure for CodeIgniter is very easy. You can download CodeIgniter by following these...

2 minutes read.

Image Manipulation Codeigniter

Image Manipulation A Codeigniter provides an image manipulation class that is used for resizing, cropping, rotation, and many other manipulation tasks. This class also support three major image libraries such as GD/GD2, NetPBM, and...

11 minutes read.

Insert data to the database CodeIgniter

After successfully creating a database connection with the CodeIgniter application, we will now understand how we can insert records into the database. To insert a record into a database table, Codeigniter provides...

8 minutes read.

CodeIgniter Security Helper

A Security helper file contains some predefined functions that are used to protect application from unauthorized access. Loading the Helper The following syntax is used to load the security helper in the CodeIgniter application. Syntax $this-> load->...

4 minutes read.

CodeIgniter Models

Models are the classes that deals with backend operations. It is used to fetch the data from the database and send it to the Controller. All the database related information provided inside...

5 minutes read.

CodeIgniter Array Helper

The Array Helper contains some predefined functions that are used to perform the various operation with array. Load Array helper It is used to load the helper class. We can pass the helper function in the...

6 minutes read.

CodeIgniter Shopping Cart Class

It contains the various function that enables the user to add or update, display, and delete data item from the shopping carts while browsing the site. The items added in the cart...

10 minutes read.

CodeIgniter CAPTCHA Helper

A CAPTCHA (Complete Automated Public Turning test to tell Computer and Humans Apart) helper is a function that is used in web application to check whether the user is human or machine. It...

3 minutes read.

How to remove an index.php file from the Codeigniter URLs?

As we know, a URL in CodeIgniter is designed in such a way that it would be search engine and human friendly, too, rather than using a standard approach to create a...

2 minutes read.

CodeIgniter Input Class

Input Class The CodeIgniter framework provides an input class that includes various input functions to pre-process global input data for protection; furthermore, it also provides some helper functions to obtain input data. This...

11 minutes read.

CodeIgniter Benchmarking Class

The CodeIgniter contains the Benchmarking class that is used to calculate the time difference between two points or the memory usage of the passed statements in the application. By default, the Benchmarking...

7 minutes read.

Update a Record CodeIgniter

Update a record In this topic, we will learn how we can update existing records in database tables. To update a record, CodeIgniter provides an update() function used with the set()...

10 minutes read.

Structure of Codeigniter

The File structure of CodeIgniter is divided into three parts: ApplicationSystemuser_guide Application -: As the name represents Application, which contains all the main parts of the project like a controller, libraries, view, models, and others....

3 minutes read.

CodeIgniter Creating Library

As we know, CodeIgniter has a large collection of library functions that are stored in the system/libraries folder. CodeIgniter also provides some additional functionality in library files. For example, if you want to...

3 minutes read.