×

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 Libraries

The library is an important part of the CodeIgniter framework. It has a large collection of libraries files that are used to increase the performance of an application. By default, all CodeIgniter libraries...

4 minutes read.

Download Helper Codeigniter

Download Helper Codeigniter: The download helper function is used to download data from the server to your computer. The data can be any format like text, jpg, mp3, mp4, etc. Loading...

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

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 Language Class

Language Class The CodeIgniter’s language class contains the various functionality that is used to access the language-specific files and text line for the purposes of internationalization. This class provides you suitability to add or...

4 minutes read.

CodeIgniter Number helper

The number helper file contains some predefined function that deals with numeric data to display numbers in bytes format. Load a Number Helper You must load a number helper file in the controller to perform...

3 minutes read.

CodeIgniter Pagination Class

Pagination Class The CodeIgniter framework provides a pagination class that is used in the data list to create pagination links in a web application. Generally, it is used to retrieve data records from...

10 minutes read.

CodeIgniter Form Helper

A form helper file contains different functions that works with forms. It is used to create various functionality in a form such as dropdown, radio button, password, upload, hidden data, etc. These functionalities...

12 minutes read.

Codeigniter Session Library

Session Library The session is an essential part of any application. It contains various functions to manage the users' status and track their activity while browsing on the site. For example,...

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

CodeIgniter File Helper

The file helper function is used to perform various tasks with files such as accessing files, writing data to files, deleting files, and more. Loading the File Helper Before using the file helper, you must...

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

Calendaring Class CodeIgniter

Calendaring Class The calendar library contains various functions that are used to create a dynamic calendar. It also enables you to pass the data to calendar cells and 100% control over the calendar...

6 minutes read.

CodeIgniter Smiley Helper

The smiley helper file contains smiley images that are used in the application for showing emotions and comments while chatting with others. Load a Smiley Helper Before using Smiley Helper in the CodeIgniter application, you...

4 minutes read.

CodeIgniter Encrypt Class

The Codeigniter provides an encrypt class used to protect confidential data stored on computer systems or data transmitted over a network. It provides two-way data encryption techniques such as Mcrypt PHP extension and...

4 minutes read.

CodeIgniter ZIP Encoding Class

ZIP Encoding Class The CodeIgniter provides a Zip encoding class that are used to compress a large file into a Zip archives. And this archives files can be downloaded either to your desktop or...

5 minutes read.

CodeIgniter Calendaring Library

The calendar library contains various functions that are used to create a dynamic calendar. It also enables you to pass the data to calendar cells and 100% control over the calendar design. Load...

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

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