How can I have CodeIgniter load specific pages using SSL?

前端 未结 10 1229
礼貌的吻别
礼貌的吻别 2020-11-30 06:40

How can I have CodeIgniter load specific pages using SSL? I have an apache2/mode_ssl server. mod_ssl uses a different document root than non-secure

10条回答
  •  渐次进展
    2020-11-30 07:09

    I would maintain two sets of the code and handle a forced redirect to HTTPS pages with a post_controller_constructor hook. The hook will be called before each page is rendered to check if the visitor should be redirected to HTTPS based on their current location on your site.

    STEP 1

    Create file: system/application/hooks/SecureAccount.php

    obj =& get_instance();
        }
    
        //--------------------------------------------------
        //Redirect to https if in the account area without it
        function index()
        {
            if(empty($_SERVER["HTTPS"]) || $_SERVER["HTTPS"] !== 'on')
            {
                $this->obj =& get_instance();
                $this->obj->load->helper(array('url', 'http'));
    
                $current = current_url();
                $current = parse_url($current);
    
                if((stripos($current['path'], "/account/") !== false))
                {
                    $current['scheme'] = 'https';
    
                    redirect(http_build_url('', $current), 'refresh');
                }
            }
        }
    }
    ?>
    

    STEP 2

    Customize the path in the function for which areas of your site should be forced to use HTTPS.

    STEP 3

    Add the following to system/application/config/hooks.php

    /* Force HTTPS for account area */
    $hook['post_controller_constructor'] = array(
                                    'class'    => 'SecureAccount',
                                    'function' => 'index',
                                    'filename' => 'SecureAccount.php',
                                    'filepath' => 'hooks',
                                    'params'   => array()
                                    );
    

提交回复
热议问题