Home / PHP/MySQL Tutorials / Archive by category 'Codeigniter'

Codeigniter

Codeigniter escape_like_str() function

This function is useful when strings are used in the LIKE portion of your query. This enables the wildcards to be correctly escaped along with the string. Here’s
an example of its usage:

$like = 'w3m' ;
$sql = " SELECT id FROM table
  WHERE column LIKE ' %" . $this- >db->escape_like_str($like) . "%' ";
$this->db- >query($sql) ;

Codeigniter escape_str() function

This function will escape all data, regardless of type.

$sql = " INSERT INTO table (name)
  VALUES(" . $this->db->escape_str( $name) . " ) " ;
$this->db- >query($sql) ;

Codeigniter escape() function

This function will determine the data type so that it can properly escape only string data. It will automatically add single quotes around the data so you don’t have to.
Here’s how you would use it:

$sql = " INSERT INTO table (name)
  VALUES(" . $this->db->escape($name) . ") ";
$this->db- >query($sql) ;

Create a hashing plugin in PHP codeigniter

We will start by creating a new file inside the system/application/plugins/ folder, called hash_pi.php.
Save the following code into the file. The logic is to split the password by half and use it as the point to insert the salt in encrypting the password.

<?php
function hash_password( $password, $salt)
{
$password_length = length($password) ;
$split_at = $password_length / 2;
$password_array = str_split( $password, $split_at) ;
$hash = sha1( $password_array[0 ] . $salt . $password_array[1] ) ;
return $hash;
}
?>

To use the plugin, you have to load it into one of your Controllers, as follows:

$this->load->plugin( ' hash' ) ;

Then you can simply use the function as:

$hashed_password = hash_password($password, $salt) ;

Hash a password using SHA1 in PHP Codeigniter

To use the sha1 function we should load the CodeIgniter Encryption Library. This is the Library that holds the sha1 function.

$this->load->library(' encryption' ) ;
$password = 'asd1234' ;
$hash = $this->encrypt- >sha1($password) ;

Allowed characters in URL in Codeigniter

CodeIgniter only allows certain characters in your URIs. You can change this in your application/config/config.php file.
Allowed characters by default:

Lowercase and Uppercase letters
Numbers (0—9)
Tilde (~)
Underscore (_)
Dash (-)
Period (.)
Colon (:)


Twitter_oauth class in codeigniter

The following code loads the oAuth library and sets up a set of variables for us to store certain information in.

<?php
require_once(APPPATH.'libraries/twitter/OAuth.php') ;
class Twitter_oauth
{
  var $consumer;
  var $token;
  var $method;
  var $http_status;
  var $last_api_call;
}
?>

$consumer— it is used to store the credentials for our application keys and the user tokens.
$token— this is used to store the user credentials. A new instance of the oAuth class OAuthConsumer is created and stored in this variable.
$method— this is used to store the oAuth Signature Method (the way we sign our oAuth calls).
$http_status and $last_api_call, are used to store the last HTTP Status Code and the URL of the last API call, respectively. These two variables are used solely for debugging purposes.


Validate an email address using PHP Codeigniter

$email = $this->input->post('email') ;
if( ! valid_email($email) )
{
  show_404 ("The email address provided is not a valid email." ) ;
}

Checking the values of form post in PHP Codeigniter

$name = $this->input->post(' name' ) ;
$email = $this- >input->post( ' email' ) ;
$subj ect = $this->input->post( ' subject' ) ;
$message = $this->input->post( ' message' ) ;
if( empty($name) OR empty( $email) OR empty($subject) OR empty($message) )
{
  show_404 ("The form submitted left fields blank, all fields are required. Please go back and fill in all of the fields. " ) ;
}

Send email in codeigniter with debugging information

$this->load->library('email' ) ;
$this->email- >from(' you@example. com' , ' Your Name' ) ;
$this->email- >to('someone@example. com' ) ;
$this->email- >cc('another@person. com' ) ;
$this->email- >bcc('theboss@example. com' ) ;
$this->email- >subject(' Email Test' ) ;
$this->email- >message(' This is a simple test we wrote for the email class. ' ) ;
$this->email- >send() ;
echo $this->email- >print_debugger() ;

Determine the user agent in PHP Codeigniter

We can use the following code to determine the user agent of the user. This will return the user agent of the user’s web browser; if one isn’t available it will return FALSE.

echo $this->input->user_agent();

Validate an IP address in PHP Codeigniter

To retrieve the user’s IP address we can use the following line of code in codeigniter

$this->input->ip_address();

If the IP address is not valid, it will return 0.0.0.0. We can use the same code to validate an ip address as shown below

$ip = "192.168.1.1";
if (!$this->input->valid_ip($ip))
{
  echo " Not a valid IP address" ;
}
else
{
  echo " Valid IP address! " ;
}

Calculate and Display memory consumption of PHP codeigniter script

Developers can display the amount of memory used by the system by including the following line of code in your application:

<?php echo $this->benchmark- >memory_usage() ; ?>

The associated psuedo-variable is

{ memory_usage}

Display total script execution time in PHP codeigniter

If you would like to display the total execution time for your application, add the following to the view.

<?php echo $this->benchmark- >elapsed_time() ; ?>

Additionally, the below pseudo-variable is provided for developers who do not wish to use pure PHP in their View files.

{ elapsed_time}

Profiling benchmarks in codeigniter

If you want your benchmarks to be available to the Profiler Class then you will need to set your benchmarks up in pairs. These pairs should end with _start and _end, but be otherwise identically named.
Example:

$this->benchmark->mark( ' first_mark_start' ) :
// something happens
$this->benchmark->mark( ' first_mark_end' ) ;
$this->benchmark->mark( ' second_mark_start' ) ;
// something else happens
$this->benchmark->mark( ' second_mark_end' ) ;

Setting multiple benchmarks using codeigniter

With the Benchmark Class, you can set multiple benchmarks and calculate the time difference between any of them.

$this->benchmark->mark( 'one' ) ;
// something happens here
$this->benchmark->mark( 'two' ) ;
// something else happens here
$this->benchmark->mark( 'three' ) ;
echo $this->benchmark->elapsed_time('one' ,  'two' ) ;
echo $this->benchmark->elapsed_time('two' , 'three' ) ;
echo $this->benchmark->elapsed_time(' one' , 'three' ) ;

we can retrieve the time between two benchmark points even if there are one or more benchmarks between them.


Setting a benchmark in codeigniter

We can set a benchmark in codeigniter by setting the starting point, setting the ending point and use a function to show the elapsed time.
Example code:

$this->benchmark->mark( ' start' ) ;
// code executes here..
$this->benchmark->mark( ' end' ) ;
echo $this->benchmark->elapsed_time(' start' , ' end' ) ;

The names start and end are arbitrary, and can be anything you choose.


Codeigniter String helper strip_quotes()

Codeigniter String helper strip_quotes()

Syntax:

strip_quotes(text);

Parameters:
Text – specifies the string to be manipulated

Return type:
string.

Description:
Removes the double quote and single quote.

Example:

$text = "w3mentor's\" Code\"";
echo strip_quotes($text);

Codeigniter String helper quotes_to_entities()

Codeigniter String quotes_to_entities()

Syntax:

quotes_to_entities(text);

Parameters:
Text – specifies the string to be manipulated

Return type:
string.

Description:
Changes the double quote and single quote to html special symbols

Example:

$text = "w3mentor's\" Code\"";
echo quotes_to_entities($text);

Codeigniter String helper trim_slashes()

Codeigniter String helper trim_slashes()

Syntax:

trim_slashes(text);

Parameters:
Text – specifies the string to be manipulated

Return type:
string.

Description:
Removes slashes at the beginning and the end of a string

Example:

$path = "/index.php/item/form/";
echo trim_slashes($path);