Some Important Codeigniter Helper Functions to Validate User Input Data

Codeigniter

Our PHP codeigniter framework has lot of functions to validate user input data. Before using this functions, you need load respective helpers.

xss_clean() –  Function filter cross-site script from a string.

Before use this function user need to load $this->load->helper('security');

Syntax : xss_clean($str);

Return string. Example

$str = 'Hello<script>alert("alert box");</script>';
echo xss_clean($str);
// result :Hello[removed]alert("alert box");[removed]

do_hash() –  Encrypting passwords.

do_hash() function do one way hashing for encrypting passwords.

Load security helper  before use the function. $this->load->helper('security');

Syntax : do_hash($password,$hash_type);

$password is required field. $hash_type is optional. By default password encrypt bt SHA1. PHP  contains a list of hash algorithms. To view all hash algorithms   print_r(hash_algos());.

Example

$password = 'admin123';

do_hash($password,md5); // the password is encrypted with md5 hashing algorithm.  

Return a string.

encode_php_tags() – Converts PHP tags to entities.

This function converts any PHP tags present in the input string.  Returns string.

Load security helper  before use the function.

$this->load->helper('security');

Syntax : encode_php_tags($str);

strip_slashes() – removes slashes from array of strings

strip_slashes() function removes slashes from input string or array of strings. Load string helper before use this function. $this->load->helper('string');

Syntax : strip_slashes($data);

For example,

$str = "String has no Slashes \"; 

echo strip_slashes($str);

//results:

String has no Slashes

reduce_multiples() – Trim a specified character repeated after directly one another.

Function trims the specified character that repeated multiple times after directly one another.

Syntax : reduce_multiples($data,$char,$trim);

If the third parameter set to TRUE, trims the character at beginning and end of the string. Load string helper to work this function. $this->load->helper('string');

Example,

$str = "one,,,two,three,four,,five";

echo reduce_multiples($str,',',true);

//results:

one,two,three,four,five

strip_quotes() – Remove quotes.

Function removes single quotes and double quotes from the input string and return the string.

Syntax : strip_quotes($str);

Load string helper to work this function. $this->load->helper('string');

Example,

$str = "string 'helper''";

echo strip_quotes($str);

//results:

string helper

Leave a Reply

Your email address will not be published. Required fields are marked *