如何创建不可访问的控制器操作?

时间:2011-05-15 22:07:59

标签: php codeigniter codeigniter-2

在我的一个控制器中,我使用codeigniter的表单验证助手。我的验证规则之一是我创建的自定义函数返回true或false。 问题是,这个验证函数是在控制器类中声明的。如何阻止用户作为控制器操作(mysite / mycontroller / my_callback_func)浏览我的验证功能?

P.S。 - 尝试将该函数设置为私有,但我收到一个错误,验证帮助程序无法访问它。

3 个答案:

答案 0 :(得分:3)

只需使用下划线启动函数/方法名称。

直接离开./system/core/CodeIgniter.php

/*
 * ------------------------------------------------------
 *  Security check
 * ------------------------------------------------------
 *
 *  None of the functions in the app controller or the
 *  loader class can be called via the URI, nor can
 *  controller functions that begin with an underscore
 */
    $class  = $RTR->fetch_class();
    $method = $RTR->fetch_method();

    if ( ! class_exists($class)
        OR strncmp($method, '_', 1) == 0
        OR in_array(strtolower($method), array_map('strtolower', get_class_methods('CI_Controller')))
        )
    {
        show_404("{$class}/{$method}");
    }

答案 1 :(得分:1)

只需在函数名称前定义函数 private 并放置下划线,例如:

class Some_class extends CI_Controller{

     private function _some_method(){}
}

答案 2 :(得分:0)

我会改为使它成为辅助函数。为此,请将文件放入system/application/myval_helper.php,然后在其中添加您的功能。然后打电话:

$this->load->helper('myval');

您将能够访问该功能。另一种方法是简单地使用.htaccess来阻止该确切的URL。

相关问题