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.

Friday, December 22, 2017

Duplicate / Copy rows/ records in the same MySQL table

I need to copy/duplicate a lot of rows. If you just export and import sql file it will not work as the table has auto increment id column. So I find an easy solution for my problem. I would like to duplicate a record in a table, but of course, the unique primary key needs to be updated.
First it need to create a temporary table and then update all id to null. then insert into table.

Here is an example query

CREATE TEMPORARY TABLE tmp SELECT * FROM sms_history WHERE date='2017-12-22';
UPDATE tmp SET id=NULL;
INSERT INTO sms_history SELECT * FROM tmp WHERE date='2017-12-22';
DROP TABLE tmp;

This is the most easy and fastest solution for this kind of problem.

Sunday, October 1, 2017

Store php variable in mysql / dynamic email, SMS template

This is a good tutorial if we want to create dynamic Email Template/ Dynamic SMS Template or dynamic content using php mysql. Storing php variable in content and save to mysql database. when fetching data from database replace variable with value dynamically.

 First create a table as your require.  Suppose we want to store this kind of sms template in datasbse

 <p>  
 Hello : [FULL_NAME] <br/>   
 username: [USER_NAME] <br/>   
 email : [USER_EMAIL] <br/>  
 website : [SITE_URL]  
 </p>  

Now we can fetch this data from database and assign in a php variable

 $content = $row['content'];  
 //replace template var with value  
 $token = array(  
   'FULL_NAME' => 'Sharif Ahmed',  
   'USER_NAME' => 'winsharif',  
   'SITE_URL' => 'http://www.esteemcorporation.com',  
   'USER_EMAIL'=> 'winsharif@test.com'  
 );  
 $pattern = '[%s]';  
 foreach($token as $key=>$val){  
   $repVar[sprintf($pattern,$key)] = $val;  
 }  
 $SMSContent = strtr($content,$repVar);  
 echo $SMSContent;  

/////////////////////////////////////Output will be //////////////////////
Hello : Sharif Ahmed 
username: winsharif
email : http://www.esteemcorporation.com 
website :  winsharif@test.com

Thursday, September 21, 2017

0.00 is not empty in empty() php

When we get value like '0.00' from database and we check if it is empty

 if(!empty(0.00)){
      return true;
}

this will return true because, it understand 0.00 is not a number it is a string.


according to php manual a variable is empty if meeting anyone of the following case:

If we want to make it / understand it as a empty then we need to use this
$amount = 0.00;

(float)$amount; 

Now this will return as empty.


  • "" (an empty string)
  • 0 (0 as an integer)
  • "0" (0 as a string)
  • NULL
  • FALSE
  • array() (an empty array)
  • var $var; (a variable declared, but without a value in a class)
  •  
  • Sunday, August 27, 2017

    Check if file / image attach in html form php

    I need to make sure that a file attached through the file input element. I tried various things, including this
    if(count($_FILES) > 0)
    if(empty($_FILES['filename']))
    if(empty($_FILES['filename']['name']))
    but all those option was not working for me.

    is_uploaded_file(). is worked but it's show error when attach multiple file. name='files[]' because it is not support array. 

    Finally the best solution is used for me 
    if($_FILES["filename"]['name'][0] != ''){
        //echo 'file attached';
    }else{
        //echo 'no file attached';
    }

    Total Pageviews