所选控制器和所选功能名称的SSL [CodeIgniter]

时间:2014-12-13 09:24:25

标签: php arrays codeigniter

我在这里遇到了严重的网站问题。我不是一个php开发人员,但我知道基础知识。所以如果我在这里犯了任何错误,请试着原谅我。

我的网站是使用CodeIgnitor构建的,我对某些选定页面使用SSL,这些页面由控制器或控制器名称或功能名称控制。

喜欢 - 我正在使用这些

的SSL
$ssl_controllers = array('controller1','controller2','controller3');

网址结构如下 - https://mysite.com/controller1

现在,我在每个控制器中都有几个功能。像这样 - 在“controller1”里面,我有“function_1()”

所以网址为 - https://mysite/controller1/function_1

现在我不需要在“controller1”

中写入“funtion_1”的SSL

如果我需要删除“function_1”的SSL,我需要从数组中删除整个“controller1”。但我不需要它,因为还有其他功能需要SSL。

那么我如何删除“function_1”的SSL?

有没有办法从数组中删除函数名?

像这样 - $ ssl_controllers =

$ssl_controllers = array('controller1'==>'function_1','controller2','controller3');

请帮助我!!

谢谢!


因为我没有使用htaccess进行重定向,所以你的方法在我的情况下不起作用。如果我使用htaccess,它会给我无限循环,无法完成!!这是因为我使用CodeIgnitor库进行重定向和特定页面SSL


这是PHP代码 -

class CI_ssl{

    function check_ssl()
    {
        $CI =& get_instance();
        $class = $CI->router->fetch_class();
        $ssl_controllers = array('controller1','controller2','controller3');

        if(in_array($class,$ssl_controllers))
        {                       
            $CI =& get_instance();
            $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
        {
            $CI =& get_instance();
            $CI->config->config['base_url'] = str_replace('https://', 'http://', $CI->config->config['base_url']);
            if ($_SERVER['SERVER_PORT'] == 443) 
                redirect($CI->uri->uri_string());
        }
    }

这为特定页面提供了SSL,并将其他重定向到仅http://

2 个答案:

答案 0 :(得分:0)

在.htaccess中使用重写规则:

RewriteCond %{SERVER_PORT} !80 
RewriteCond %{REQUEST_URI} controller1/function_1
RewriteRule ^(.*)$ http://www.yourdomain.com/controller1/function_1 [R=301,L]

注意:您的base_url必须在配置文件中设置为“/”。

答案 1 :(得分:-1)

经过今天的研究,我终于解决了我的问题。这是演示 -

$CI =& get_instance();
$class = $CI->router->fetch_class();
$ssl_controllers = array('controller1','controller2','controller3');

在这段代码中,我以这种方式调用了特定的函数 -

$CI =& get_instance();
$class = $CI->router->fetch_class();
$ssl_controllers = array('controller1','controller2','controller3');
$my_function = $CI->router->fetch_method(); // fetch method is used for getting the specific function name
$ssl_functions = array('function_1'); // specific function name in array

然后,我只是将elseif语句添加到我的病情中 -

if(in_array($my_function,$ssl_functions))
        {
            $CI =& get_instance();
            $CI->config->config['base_url'] = str_replace('https://', 'http://', $CI->config->config['base_url']);
            if ($_SERVER['SERVER_PORT'] == 443) 
                redirect($CI->uri->uri_string());
        }
        elseif(in_array($class,$ssl_controllers))
        {                       
            $CI =& get_instance();
            $CI->config->config['base_url'] = str_replace('http://', 'https://', $CI->config->config['base_url']);          
            //echo $CI->config->config['base_url']."=>".$CI->uri->uri_string()."=>".$_SERVER['SERVER_PORT'];exit;           
            if ($_SERVER['SERVER_PORT'] != 443) 
                redirect($CI->uri->uri_string());
        }
        else
        {
            $CI =& get_instance();
            $CI->config->config['base_url'] = str_replace('https://', 'http://', $CI->config->config['base_url']);
            if ($_SERVER['SERVER_PORT'] == 443) 
                redirect($CI->uri->uri_string());
        }
    }

因此,我的新网址没有SSL!像这样 -

http://mysite/controller1/function_1