×

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

CodeIgniter provides an e-mail library that helps to send a message from one application to another. You can create and send text messages easily in CodeIgniter application even you can set email...

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

CodeIgniter Output class

Output class The CodeIgniter framework contains a core class that is an Output class. It  includes the various functions that are used to send the final web page to the requesting browser. In...

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

CodeIgniter Helpers

The Helpers (functions) are stored in the Helper file. It helps CodeIgniter to perform different tasks. The helper file is the collection of many functions that is used to perform specific task. It...

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

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

The HTML helper function is used in CodeIgniter to provide various functionality, such as headings, images, links, list tags, etc. Loading the HTML Helper Before using the html helper function, you must load the...

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

Versions of CodeIgniter

CodeIgniter v3.1.11 (This is the current version which we will used in this tutorial)CodeIgniter v3.1.10CodeIgniter v3.1.9CodeIgniter v3.1.8CodeIgniter v3.1.7CodeIgniter v3.1.6CodeIgniter v3.1.5CodeIgniter v3.1.4CodeIgniter v3.1.3CodeIgniter v3.1.2CodeIgniter v3.1.1CodeIgniter v3.1.0CodeIgniter v3.0.6CodeIgniter...

1 minute 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 Cookie Helper

A cookie is a small set of files sent from the web server to the end-user system. In Helpers, the cookie helper file has some predefined functions that are used to the...

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