Showing posts with label codeingniter. Show all posts
Showing posts with label codeingniter. Show all posts

Tuesday, December 26, 2017

Force to use/redirect https / SSL Codeigniter

There are two easy way to redirect/force to use https / SSL in Codeigniter.
1. Using .htaccess
2. Using codeigniter hook function.

Method One
1. Change base_url in your config from application/config/config.php file:
     $config['base_url'] = 'https://www.my-site.com/';

2. Redirect incoming traffic from http to https:
    RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Method Two

Open config file from location application/config/config.php and enable or set hooks to true like this:
$config['enable_hooks'] = TRUE;
Then create a new file named hooks.php inside the config folder (i.e. application/config/hooks.php) and add the following code in it:
$hook['post_controller_constructor'][] = array(
    'function' => 'redirect_ssl',
    'filename' => 'ssl.php',
    'filepath' => 'hooks'
);
Now create a new directory named hooks inside the application folder (i.e. application/hooks) and then create a new file named ssl.php inside the hooks folder (i.e. application/hooks/ssl.php).
Add the following code in the ssl.php file:

function redirect_ssl() {
    $CI =& get_instance();
    $class = $CI->router->fetch_class();
    $exclude =  array('client');  // add more controller name to exclude ssl.
    if(!in_array($class,$exclude)) {
        // redirecting to ssl.
        $CI->config->config['base_url'] = str_replace('http://', 'https://', $CI->config->config['base_url']);
        if ($_SERVER['SERVER_PORT'] != 443) redirect($CI->uri->uri_string());
    } else {
        // redirecting with no ssl.
        $CI->config->config['base_url'] = str_replace('https://', 'http://', $CI->config->config['base_url']);
        if ($_SERVER['SERVER_PORT'] == 443) redirect($CI->uri->uri_string());
    }
}

In conclusion method one is the best and fasted.

Wednesday, May 24, 2017

Image resize when uploading Codeingniter 3

Image resize when uploading Codeingniter 3

We must enable file upload and fileinfo extensions.

 if(!empty($_FILES['image']['name'])){  
         $config['upload_path'] = './uploads/onlineadmission/';  
                     $config['allowed_types'] = 'gif|jpg|png';  
                     $config['max_size']   = '1500';  
                     $config['encrypt_name'] = TRUE;  
         $this->load->library('upload', $config);  
           if (!$this->upload->do_upload('image')){  
               $filerror = array('filerror' => $this->upload->display_errors('',''));  
               $this->data['filerror'] = $filerror;   
               $picture = '';             
           }  
           else{  
               $data_upload =$this->upload->data();  
               $picture = $data_upload['file_name'];  
                                    $configer = array(  
                                     'image_library'  => 'gd2',  
                                     'source_image'  => $data_upload['full_path'],  
                                     'maintain_ratio' => TRUE,  
                                     'width'      => 200,  
                                     'height'     => 200,  
                                    );  
                                    $this->image_lib->clear();  
                                    $this->image_lib->initialize($configer);  
                                    $this->image_lib->resize();  
           }  
       }else{  
         $picture = '';  
       }    

Total Pageviews