Category Archives: Codeigniter

Codeigniter encryption library example


Codeigniter encryption library example : $this->load->library(‘encrypt’); is used for encryption and decryption in codeigniter.


1. first Add encryption key in config.php file.

$config['encryption_key'] = "YOUR ENC KEY";

2. Load Library : $this->load->library(‘encrypt’);

3. Now you can encode/Decode Your message as Below

Encode :

$this->encrypt->encode($msg)

or 
$key = "Your more secure key";
$this->encrypt->encode($msg,$key)

Decode:

$this->encrypt->decode($msg)

or 
$key = "Your more secure key";
$this->encrypt->decode($msg,$key)

Codeigniter get affected rows insert example


Codeigniter get affected rows insert example : $this->db->affected_rows(); is used to get affected rows in codeigniter.


Codeigniter get affected rows insert example

$data = array(
               'id' => $id,
               'name' => $name,
               'email' => $email,
               'phone' => $phone
            );

$this->db->insert('table_name', $data);
$affected_rows = $this->db->affected_rows();

Will Return no of rows inserted.

Codeigniter get affected rows delete example


Codeigniter get affected rows delete example : $this->db->affected_rows(); is used to get affected rows in codeigniter.


Codeigniter get affected rows delete example

$where_array = array(
                'id'=>$id
            );

$this->db->delete('table_name', $where_array);
$affected_rows = $this->db->affected_rows();

Codeigniter get affected rows update example


Codeigniter get affected rows update example : $this->db->affected_rows(); is used to get affected rows in codeigniter.


$data = array(
               'name' => $name,
               'email' => $email,
               'phone' => $phone
            );

$this->db->where('id', $id);
$this->db->update('table_name', $data);
$affected_rows = $this->db->affected_rows();

Codeigniter enable error log display reporting


Codeigniter enable error log display reporting


You Can Enable Error Reporting in Codeigniter by using the following settings:
Go to the index.php file which is located at the root of your application.
And Change the environment to development as
define(‘ENVIRONMENT’, ‘development’);

    define('ENVIRONMENT', 'development');
/*
 *---------------------------------------------------------------
 * ERROR REPORTING
 *---------------------------------------------------------------
 *
 * Different environments will require different levels of error reporting.
 * By default development will show errors but testing and live will hide them.
 */

if (defined('ENVIRONMENT'))
{
    switch (ENVIRONMENT)
    {
        case 'development':
            error_reporting(E_ALL);
        break;

    case 'testing':
    case 'production':
        error_reporting(0);
    break;

    default:
        exit('The application environment is not set correctly.');
}
}

Which will solve your problem.

Codeigniter clear database cache


$this->db->cache_delete_all(); is used to clear all cache files.


Codeigniter clear database cache Example

$this->db->cache_delete_all();

Will clear All cache.

Codeigniter database cache


Codeigniter database cache : Database caching allows you to cache your query results as text file to reduce the server load.


Codeigniter database cache example


$this->db->cache_on()
is used to enable database caching.


$this->db->cache_on();

$query = $this->db->query("seelct * from users");


If you are using multiple queries and you want to enable for some of them you can use $this->db->cache_off();


$this->db->cache_on();

$query = $this->db->query("seelct * from users");

$this->db->cache_off();

$query = $this->db->query("seelct * from profile");

$this->db->cache_on();

$query = $this->db->query("seelct * from city");

Codeigniter get current ip address


Codeigniter get current ip address We can use input class to get the customer ip address. Input class provides inbuilt functions to get and validate ip address. Here in this tutorial we are going to explain how you can get ip address using input class in Codeigniter.


Codeigniter get current ip address Example

Codeigniter get current ip address

You can get ip address using the following syntax-

$this->input->ip_address();

The above syntax will return the current user’s ip and if ip addres is invalid it returns 0.0.0.0.

The input class is automatically loaded so need not to load manually, Just use – $this->input->ip_address(); to get the ip address.

Validate Ip Address

You can validate ip address using the below syntax-

$ip = "Your_IP";
$this->input->valid_ip($ip);

Pass the IP address to validate in valid_ip() function. It will return true if IP is valid and return false if IP is invalid.

Example

Here is an example of validate ip address.

$ip ='109.77.98.92';
$isValidIp = $this->input->valid_ip($ip);
if($isValidIp){
echo "Ip is valid";
}else{
echo "Ip is Not Valid";
}

Codeigniter redirect refresh


Codeigniter redirect refresh – Redirect funciton is used to “header redirect” to the specified URL. To redirect refresh you need to pass second parameter as “refresh” . You need to load url helper to use this function so make sure you have already loaded this helper. Here in this article we are going to explain how you can use redirect function to redirect on specified url.


Codeigniter redirect refresh Syntax

Note : $this->load->helper(‘url’); should be loaded before redirect.


redirect("location","refresh");

Where First option is location ie . URI and second option is optional.
If you want to redirect refresh use second parameter.

Codeigniter redirect Example


redirect("/post/66","refresh");