×

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 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 URL Helper

The helper file contains some predefined functions that are used to work with URL, such as generate the current file path, transfer control, redirect, create a link, etc. Load the URL Helper Before using...

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

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

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.

CodeIgniter Controller

As its name suggest that it is a Controller that controls the web application, and it works as an intermediary between the Models and the View. A controller takes requests from the user...

3 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 Routes

The Routes are used to handle URL requests on the browser. It matches the URL request to the predefined routes in CodeIgniter. Therefore, a file must be predefined as a routes.php file to...

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

CodeIgniter Date helper

A date helper file contains some predefined functions that work with date functions. Before using the date function in CodeIgniter, you have to load the date helper in the controller. For example: $this->load->helper(‘date’); Functions of...

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

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.

Delete a record from Database CodeIgniter

Delete a record from the Database After inserting, retrieving, and updating table records, we will now learn how we can delete a particular record from a database table. To delete a record, first, we...

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

Directory Helper Codeigniter

Directory Helper CodeIgniter: provides a directory helper function that works with the directory. It shows the structure of the file, hidden file in the folder, and the folder in...

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